linux下用C语言写MySQL程序,用如下命令编译时出错:
[xitong@localhost mysql]$ gcc -o mysql mysql.c -lmysqlclient
错误如下:
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld 返回 1
解决方法:
用locate命令查找mysqlclient所在目录,如下所示:
[xitong@localhost mysql]$ locate mysqlclient
结果显示:
/usr/local/mysql/lib/mysql/libmysqlclient.a
/usr/local/mysql/lib/mysql/libmysqlclient.la
/usr/local/mysql/lib/mysql/libmysqlclient.so
/usr/local/mysql/lib/mysql/libmysqlclient.so.16
/usr/local/mysql/lib/mysql/libmysqlclient.so.16.0.0
/usr/local/mysql/lib/mysql/libmysqlclient_r.a
/usr/local/mysql/lib/mysql/libmysqlclient_r.la
/usr/local/mysql/lib/mysql/libmysqlclient_r.so
/usr/local/mysql/lib/mysql/libmysqlclient_r.so.16
/usr/local/mysql/lib/mysql/libmysqlclient_r.so.16.0.0
编译时加入路径/usr/local/mysql/lib/mysql,如下:
[xitong@localhost mysql]$ gcc -o mysql mysql.c -L/usr/local/mysql/lib/mysql -lmysqlclient
[xitong@localhost mysql]$
编译通过,问题解决。
再加一点,编译问题解决了,运行的时候发现出现如下错误:
./mysql: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
借用一网友的解决方法,添加环境变量:
[xitong@localhost mysql]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib/mysql/
再次运行编译好的程序,OK