当前位置: 技术问答>linux和unix
linux环境下用c api成功连接过mysql的请进
来源: 互联网 发布时间:2015-02-12
本文导语: 源代码为: #include #include "mysql.h" void print_error(MYSQL *conn,char *errstr) { fprintf(stderr,"%s:nError %u(%s)n",errstr, mysql_errno(conn),mysql_error(conn)); exit(1); } int main() { MYSQL *conn; conn=mysql_init(NULL); if(mysql_real_connect(conn,"localhost","...
源代码为:
#include
#include "mysql.h"
void print_error(MYSQL *conn,char *errstr)
{
fprintf(stderr,"%s:nError %u(%s)n",errstr,
mysql_errno(conn),mysql_error(conn));
exit(1);
}
int main()
{
MYSQL *conn;
conn=mysql_init(NULL);
if(mysql_real_connect(conn,"localhost","root","","test",3307,NULL,0)==NULL)
{
print_error(conn,"mysql_real_connect() failed:");
}
else
{
printf("Hello!n");
}
}
编译指令为
gcc -o my my.c -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient -lz
编译通过,执行时错误提示如下:
error while loading shared libraries:libmysqlclient.so.10:cannot open
object file :no such file or directory.
而我在/usr/local/mysql/lib 目录下能找到libmysqlclient.so.10文件,
究竟是什么原因,我要急了
#include
#include "mysql.h"
void print_error(MYSQL *conn,char *errstr)
{
fprintf(stderr,"%s:nError %u(%s)n",errstr,
mysql_errno(conn),mysql_error(conn));
exit(1);
}
int main()
{
MYSQL *conn;
conn=mysql_init(NULL);
if(mysql_real_connect(conn,"localhost","root","","test",3307,NULL,0)==NULL)
{
print_error(conn,"mysql_real_connect() failed:");
}
else
{
printf("Hello!n");
}
}
编译指令为
gcc -o my my.c -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqlclient -lz
编译通过,执行时错误提示如下:
error while loading shared libraries:libmysqlclient.so.10:cannot open
object file :no such file or directory.
而我在/usr/local/mysql/lib 目录下能找到libmysqlclient.so.10文件,
究竟是什么原因,我要急了
|
定义一个环境变量
LD_LIBRARY_PATH = /usr/local/mysql/lib
或者把/usr/local/mysql/lib 下的文件都拷贝到/usr/lib下
LD_LIBRARY_PATH = /usr/local/mysql/lib
或者把/usr/local/mysql/lib 下的文件都拷贝到/usr/lib下