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

问题

I have the following code

#include

#include

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

回答1:

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.

回答2:

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

$ 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

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!

回答3:

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

sudo apt install libmysqlclient-dev

the error went away.

回答4:

I had the same problem, but fortunately the command

sudo apt install libmysqlclient-dev as specified by

user4713908 fixed it.

I juat had to specify the path in my makefile as

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

I'm using Kali Linux

来源:https://stackoverflow.com/questions/20909685/fatal-error-mysql-hno-such-file-or-directory-during-compilation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值