致命错误mysql。h:编译过程中没有这样的文件或目录。

时间:2021-09-22 18:31:25

I have the following code

我有以下代码。

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{
  printf("MySQL client version: %s\n", mysql_get_client_info());

  exit(0);
}

when i try to compile it using

当我尝试编译它时。

gcc mysqldb.c -o mysql -I/usr/include/mysql -lmysqlclient 

i get an error saying fatal error mysql.h:No such file or directory. how can i successfully compile and run the code

我犯了一个错误,说mysql错误。h:没有这样的文件或目录。如何成功地编译和运行代码?

4 个解决方案

#1


4  

Check that /usr/include/mysql/mysql.h exists. If you have installed the header files somewhere else (say /opt/mysql/include), add that location with -I/opt/mysql/include.

检查/usr/include/mysql/mysql.h的存在。如果您已经在其他地方安装了头文件(比如/opt/mysql/include),那么可以添加-I/opt/mysql/include。

#2


5  

I do not know if there is some variation in your operation system. Mine is Arch Linux, and I have installed mariaDB. Within the package, there is a program called 'mysql_config' which can provide the right way of compile your program. By runnig

我不知道你的操作系统是否有变化。我的是Arch Linux,我已经安装了mariaDB。在这个包中,有一个名为“mysql_config”的程序,它可以提供编译程序的正确方法。通过运行方式

$ mysql_config --help
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
    --cflags         [-I/usr/include/mysql]
    --include        [-I/usr/include/mysql]
    --libs           [-L/usr/lib -lmysqlclient -lpthread -lz -lm -lssl   -lcrypto -ldl]
    --libs_r         [-L/usr/lib -lmysqlclient_r -lpthread -lz -lm -lssl -lcrypto -ldl]
    --plugindir      [/usr/lib/mysql/plugin]
    --socket         [/run/mysqld/mysqld.sock]
    --port           [0]
    --version        [10.0.17]
    --libmysqld-libs [-L/usr/lib -lmysqld]
    --variable=VAR   VAR is one of:
            pkgincludedir [/usr/include/mysql]
            pkglibdir     [/usr/lib]
            plugindir     [/usr/lib/mysql/plugin]

you can see the control flags of the program. With your program, i used the following:

您可以看到程序的控制标志。在您的项目中,我使用了以下几点:

$gcc main.c -o main $(mysql_config --libs --cflags)

and then, by runnig the the new program 'main'

然后,通过runnig的新程序“main”

$./main
MySQL client version: 10.0.17-MariaDB

which clearly worked out!

这很明显了!

So, i'm sure that there a few others ways of doing this, but now this is fine for me.

所以,我确信还有其他一些方法可以做到这一点,但现在这对我来说很好。

Tip

Run the command

运行以下命令

$mysql_config --libs --cflags

to see the exacly flags that mysql_config produces. Enjoy!

要查看mysql_config生成的精确标志。享受吧!

#3


2  

I come across the same error today, turn out that I forget to install package libmysqlclient-dev. After I install it with

我今天遇到了同样的错误,我忘记了安装包libmysqlclient-dev。在我安装之后。

sudo apt install libmysqlclient-dev

the error went away.

错误的走了。

#4


0  

I had the same problem, but fortunately the command sudo apt install libmysqlclient-dev as specified by user4713908 fixed it.

我遇到了同样的问题,但幸运的是,命令sudo apt安装了libmysqlclient-dev,这是user4713908所指定的。

I juat had to specify the path in my makefile as

我必须在makefile中指定路径。

gcc -o database1 chapter5_1.c -I/usr/include/mysql

I'm using Kali Linux

我使用Linux卡莉

#1


4  

Check that /usr/include/mysql/mysql.h exists. If you have installed the header files somewhere else (say /opt/mysql/include), add that location with -I/opt/mysql/include.

检查/usr/include/mysql/mysql.h的存在。如果您已经在其他地方安装了头文件(比如/opt/mysql/include),那么可以添加-I/opt/mysql/include。

#2


5  

I do not know if there is some variation in your operation system. Mine is Arch Linux, and I have installed mariaDB. Within the package, there is a program called 'mysql_config' which can provide the right way of compile your program. By runnig

我不知道你的操作系统是否有变化。我的是Arch Linux,我已经安装了mariaDB。在这个包中,有一个名为“mysql_config”的程序,它可以提供编译程序的正确方法。通过运行方式

$ mysql_config --help
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
    --cflags         [-I/usr/include/mysql]
    --include        [-I/usr/include/mysql]
    --libs           [-L/usr/lib -lmysqlclient -lpthread -lz -lm -lssl   -lcrypto -ldl]
    --libs_r         [-L/usr/lib -lmysqlclient_r -lpthread -lz -lm -lssl -lcrypto -ldl]
    --plugindir      [/usr/lib/mysql/plugin]
    --socket         [/run/mysqld/mysqld.sock]
    --port           [0]
    --version        [10.0.17]
    --libmysqld-libs [-L/usr/lib -lmysqld]
    --variable=VAR   VAR is one of:
            pkgincludedir [/usr/include/mysql]
            pkglibdir     [/usr/lib]
            plugindir     [/usr/lib/mysql/plugin]

you can see the control flags of the program. With your program, i used the following:

您可以看到程序的控制标志。在您的项目中,我使用了以下几点:

$gcc main.c -o main $(mysql_config --libs --cflags)

and then, by runnig the the new program 'main'

然后,通过runnig的新程序“main”

$./main
MySQL client version: 10.0.17-MariaDB

which clearly worked out!

这很明显了!

So, i'm sure that there a few others ways of doing this, but now this is fine for me.

所以,我确信还有其他一些方法可以做到这一点,但现在这对我来说很好。

Tip

Run the command

运行以下命令

$mysql_config --libs --cflags

to see the exacly flags that mysql_config produces. Enjoy!

要查看mysql_config生成的精确标志。享受吧!

#3


2  

I come across the same error today, turn out that I forget to install package libmysqlclient-dev. After I install it with

我今天遇到了同样的错误,我忘记了安装包libmysqlclient-dev。在我安装之后。

sudo apt install libmysqlclient-dev

the error went away.

错误的走了。

#4


0  

I had the same problem, but fortunately the command sudo apt install libmysqlclient-dev as specified by user4713908 fixed it.

我遇到了同样的问题,但幸运的是,命令sudo apt安装了libmysqlclient-dev,这是user4713908所指定的。

I juat had to specify the path in my makefile as

我必须在makefile中指定路径。

gcc -o database1 chapter5_1.c -I/usr/include/mysql

I'm using Kali Linux

我使用Linux卡莉