ubuntu下执行c文件找不到mysql.h,dev已装 如图,whereis mysql.h显示存 ...
发布网友
发布时间:2022-04-29 02:26
我来回答
共1个回答
热心网友
时间:2022-04-08 00:32
今天看着看着代码,突然就想学学linux下c中怎么连接mysql数据库的。带着好奇的心理编写了,最最简单的连接程序:
#include <stdio.h>
#include <mysql.h>
MYSQL *mysql;//首先要定义个mysql对象,来实现初始化。
main (void){
mysql_init(&mysql);//初始化mysql数据库
if(mysql_real_connect(&mysql,"localhost","root","123qwe","test",3306,0,0)){//此方法是用于连接mysql数据库的,详细参数请参照:http://dev.mysql.com/doc/refman/5.1/zh/apis.html#mysql-real-connect
printf("OK\n");//成功,输出OK
return 0;
}
else {
printf("ERORR\n");//失败输出ERORR
}
}
简单编译下:gcc -0 test_sql test_sql.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
悲催的来了:test_sql.c:2: fatal error: mysql.h: 没有那个文件或目录.
居然告诉我说没有mysql.h这个文件,可是实验了,我确实安装了mysql了啊。。。。。。。
我想可能是缺少lib库了吧,百度啊,google啊,一顿搜,哦!~
原来是缺少libmysqlclient-dev,OK安装就是了,apt-get install libmysqlclient-dev
继续编译,结果:
test_sql.c: In function ‘main’:
test_sql.c:7: warning: passing argument 1 of ‘mysql_init’ from incompatible pointer type
/usr/include/mysql/mysql.h:434: note: expected ‘struct MYSQL *’ but argument is of type ‘struct MYSQL **’
test_sql.c:8: warning: passing argument 1 of ‘mysql_real_connect’ from incompatible pointer type
/usr/include/mysql/mysql.h:441: note: expected ‘struct MYSQL *’ but argument is of type ‘struct MYSQL **’追问
所以说我已经装了dev了