我是在Ubuntu系统下测试的
此系统下,mysql安装: apt-get install mysql-server 等了有10多分钟
然后又装了mysql-client: apt-get install mysql-client
还不够还需要mysql的开发包: apt-get install libmysql++
没有权限的话sudo 你懂的
敲下代码
#include <stdlib.h> #include "mysql.h" int main(void){ MYSQL *conn_ptr; conn_ptr=mysql_init(NULL); if(!conn_ptr){ fprintf(stderr,"mysql_init failed \n"); return EXIT_FAILURE; } conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","111111","mysql",0,NULL,0); if(conn_ptr){ printf("Connection success\n"); }else{ printf("Connection failed\n"); } mysql_close(conn_ptr); return EXIT_SUCCESS; }
编译
gcc -o testmysql testmysql.c
testmysql.c:4:19: fatal error: mysql.h: No such file or directory
compilation terminated.
然后修改gcc -o testmysql testmysql.c -I/usr/include/mysql/
/tmp/ccW7qMov.o: In function `main': testmysql.c:(.text+0x11): undefined reference to `mysql_init' testmysql.c:(.text+0x8f): undefined reference to `mysql_real_connect' testmysql.c:(.text+0xc0): undefined reference to `mysql_close' collect2: ld returned 1 exit statu
最后修改
gcc -o testmysql testmysql.c -I/usr/include/mysql/ -lmysqlclient -Wall -g
成功