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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20909685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:23:25  来源:igfitidea点击:

fatal error mysql.h:No such file or directory during compilation

c++mysqlcompiler-construction

提问by user2883880

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:没有这样的文件或目录。我怎样才能成功编译和运行代码

回答by

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”的程序,它可以提供编译程序的正确方法。由 runnig

$ 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'

然后,通过运行新程序“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 产生的确切标志。享受!

回答by 3bu1

Check that /usr/include/mysql/mysql.hexists. If you have installed the header filessomewhere 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.

回答by fengqi

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

我今天遇到了同样的错误,结果是我忘记安装 package libmysqlclient-dev。在我安装它之后

sudo apt install libmysqlclient-dev

the error went away.

错误消失了。

回答by Mahamadou Kone

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

我遇到了同样的问题,但幸运的sudo apt install 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

我正在使用Kali Linux