fatal error: mysql.h: No such file or directory

   轉自: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 你懂的

      敲下代碼

C代碼  收藏代碼
  1. #include <stdlib.h>  
  2.   
  3. #include "mysql.h"  
  4.   
  5. int main(void){  
  6.         MYSQL *conn_ptr;  
  7.   
  8.         conn_ptr=mysql_init(NULL);  
  9.         if(!conn_ptr){  
  10.                 fprintf(stderr,"mysql_init failed \n");  
  11.                 return  EXIT_FAILURE;  
  12.         }  
  13.   
  14.         conn_ptr  = mysql_real_connect(conn_ptr,"localhost","root","111111","mysql",0,NULL,0);  
  15.   
  16.         if(conn_ptr){  
  17.                 printf("Connection success\n");  
  18.         }else{  
  19.                 printf("Connection failed\n");  
  20.         }  
  21.   
  22.         mysql_close(conn_ptr);  
  23.         return EXIT_SUCCESS;  
  24. }  

 編譯

gcc -o testmysql testmysql.c

Java代碼  收藏代碼
  1. testmysql.c:4:19: fatal error: mysql.h: No such file or directory  
  2. compilation terminated.  

 

然後修改gcc -o testmysql testmysql.c  -I/usr/include/mysql/

C代碼  收藏代碼
  1. /tmp/ccW7qMov.o: In function `main':  
  2. testmysql.c:(.text+0x11): undefined reference to `mysql_init'  
  3. testmysql.c:(.text+0x8f): undefined reference to `mysql_real_connect'  
  4. testmysql.c:(.text+0xc0): undefined reference to `mysql_close'  
  5. collect2: ld returned 1 exit statu  

 

最後修改

C代碼  收藏代碼
  1. gcc -o testmysql testmysql.c -I/usr/include/mysql/ -lmysqlclient -Wall -g  

成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章