转自:http://tangmingjie2009.iteye.com/blog/1521088
我是在Ubuntu系统下测试的
此系统下,mysql安装: apt-get install mysql-server 等了有10多分钟
然后又装了mysql-client: apt-get install mysql-client
还不够还需要mysql的开发包: apt-get install libmysql++
没有权限的话sudo 你懂的
敲下代码
- int main(void){
- if(!conn_ptr){
- "mysql_init failed ");
- return EXIT_FAILURE;
- "localhost","root","111111","mysql",0,NULL,0);
- if(conn_ptr){
- "Connection success ");
- else{
- "Connection failed ");
- return EXIT_SUCCESS;
- }
编译
gcc -o testmysql testmysql.c
- compilation terminated.
然后修改gcc -o testmysql testmysql.c -I/usr/include/mysql/
- testmysql.c:(.text+0x11): undefined reference to `mysql_init'
- 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
成功