以下是源程序/*
* 由于编译时需要用到mysql的lib和include中的文件,因此命令为:
* gcc mysql.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz -o mysql_conn */
#include <stdio.h>
#include <stdlib.h>/*加入mysql的api的头文件*/
#include "/home/eloy/mysql/mysql-connector-c-6.0.2-linux-rhel5-x86-32bit/include/mysql.h"int main()
{
/* 变量声明 */
MYSQL myconn;
int res; mysql_init(&myconn);
if(mysql_real_connect(&myconn, "localhost","root","123456","message",0,NULL,0))
{
printf("Connect success\n");
res = mysql_query(&myconn, "INSERT INTO comment(username) VALUES('test')");
if(!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&myconn));
}
else
{
fprintf(stderr,"Insert error %d: %s\n",mysql_errno(&myconn),mysql_error(&myconn));
}
mysql_close(&myconn);
}
else
{
fprintf(stderr,"Connectiion failed\n");
if(mysql_errno(&myconn))
{
fprintf(stderr,"Connection error %d: %s\n",mysql_errno(&myconn),mysql_error(&myconn));
}
}
return 0;
}用 gcc -o connectOracl connectOracl.c -L /usr/mysql/lib/libmysqlclient.a -lz 编译
/tmp/ccYJkPCN.o: In function `main':
connectOracl.c:(.text+0x1f): undefined reference to `mysql_init'
connectOracl.c:(.text+0x65): undefined reference to `mysql_real_connect'
connectOracl.c:(.text+0x8f): undefined reference to `mysql_query'
connectOracl.c:(.text+0xa6): undefined reference to `mysql_affected_rows'
connectOracl.c:(.text+0xc6): undefined reference to `mysql_error'
connectOracl.c:(.text+0xd6): undefined reference to `mysql_errno'
connectOracl.c:(.text+0x102): undefined reference to `mysql_close'
connectOracl.c:(.text+0x137): undefined reference to `mysql_errno'
connectOracl.c:(.text+0x149): undefined reference to `mysql_error'
connectOracl.c:(.text+0x159): undefined reference to `mysql_errno'
collect2: ld returned 1 exit status用gcc -o connectOracl -g connectOracl.c -l mysqlclient 编译
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status环境变量都设好了,依然不行。
* 由于编译时需要用到mysql的lib和include中的文件,因此命令为:
* gcc mysql.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz -o mysql_conn */
#include <stdio.h>
#include <stdlib.h>/*加入mysql的api的头文件*/
#include "/home/eloy/mysql/mysql-connector-c-6.0.2-linux-rhel5-x86-32bit/include/mysql.h"int main()
{
/* 变量声明 */
MYSQL myconn;
int res; mysql_init(&myconn);
if(mysql_real_connect(&myconn, "localhost","root","123456","message",0,NULL,0))
{
printf("Connect success\n");
res = mysql_query(&myconn, "INSERT INTO comment(username) VALUES('test')");
if(!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&myconn));
}
else
{
fprintf(stderr,"Insert error %d: %s\n",mysql_errno(&myconn),mysql_error(&myconn));
}
mysql_close(&myconn);
}
else
{
fprintf(stderr,"Connectiion failed\n");
if(mysql_errno(&myconn))
{
fprintf(stderr,"Connection error %d: %s\n",mysql_errno(&myconn),mysql_error(&myconn));
}
}
return 0;
}用 gcc -o connectOracl connectOracl.c -L /usr/mysql/lib/libmysqlclient.a -lz 编译
/tmp/ccYJkPCN.o: In function `main':
connectOracl.c:(.text+0x1f): undefined reference to `mysql_init'
connectOracl.c:(.text+0x65): undefined reference to `mysql_real_connect'
connectOracl.c:(.text+0x8f): undefined reference to `mysql_query'
connectOracl.c:(.text+0xa6): undefined reference to `mysql_affected_rows'
connectOracl.c:(.text+0xc6): undefined reference to `mysql_error'
connectOracl.c:(.text+0xd6): undefined reference to `mysql_errno'
connectOracl.c:(.text+0x102): undefined reference to `mysql_close'
connectOracl.c:(.text+0x137): undefined reference to `mysql_errno'
connectOracl.c:(.text+0x149): undefined reference to `mysql_error'
connectOracl.c:(.text+0x159): undefined reference to `mysql_errno'
collect2: ld returned 1 exit status用gcc -o connectOracl -g connectOracl.c -l mysqlclient 编译
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status环境变量都设好了,依然不行。
看看,是否有
T mysql_init感觉没有装好导致的。
指定了,我重新编译了一下gcc -o connectOracl -g connectOracl.c -I /home/eloy/mysql/mysql-connector-c-6.0.2-linux-rhel5-x86-32bit/include -L /home/eloy/mysql/mysql-connector-c-6.0.2-linux-rhel5-x86-32bit/lib -l mysqlclient -lz 这样就编译过去了,只是多了一个指定include的路径。不明所以
1、可以使用
gcc -I/usr/include/mysql -lmysqlclient -o connectOracl connectOracl.c
试一下。
2、另外你要确定你的mysql的dev库安装了。如果没有安装需要下载一个,这个rpm包中包含的mysql的头文件的源码。
没装开发包!
MySQL-share
MySQL-deve
这个两个都得装!
int res; mysql_init(&myconn);
if(mysql_real_connect(&myconn, "localhost","root","123456","message",0,NULL,0))
建议这里:
MYSQL * myconn;
myconn = mysql_init(myconn);
mysql_real_connect(myconn,