mingw環境添加mysql開發庫

來源:互聯網
上載者:User

標籤:

  今天碰巧要用到mysql進行開發,在windows的mingw平台。

0.下載mysql

  http://cdn.mysql.com/Downloads/MySQL-5.1/mysql-noinstall-5.1.73-win32.zip

  下載這個綠色版的。然後解壓到D盤下,複製一份ini設定檔,修改部分選項,並啟動服務,詳細看我以前的部落格

  http://www.cnblogs.com/wunaozai/p/3641589.html

1.開發安裝完了之後就進行開發,下面這個是一個樣本程式

 1 #include <stdio.h> 2 #include <windows.h> 3 #include <mysql.h> 4 #include <winsock2.h> 5  6 int main(int argc,char *argv[]) 7 { 8     MYSQL conn; 9     int res;10     mysql_init(&conn);11     if(mysql_real_connect(&conn,"localhost","root","","test",0,NULL,CLIENT_FOUND_ROWS)) //"root":資料庫管理員 "":root密碼 "test":資料庫的名字12     {13         printf("connect success!\n");14         res=mysql_query(&conn,"insert into test values(‘user‘,‘123456‘)");15         if(res)16         {17             printf("error\n");18         }19         else20         {21             printf("OK\n");22         }23         mysql_close(&conn);24     }25     return 0;26 }

  要把mysql安裝程式中的include複製到開發環境中。然後還有構造一個libmysql.a這種靜態連結庫。
  官方mysql的win發行版是由vc編譯,產生的lib無法被mingw連結。連結時會拋出未定義錯。

  具體的辦法如下 在include檔案夾中複製 libmysql.def 到 lib目錄,在lib目錄執行下面這句

  dlltool --input-def libmySQL.def --dllname libmySQL.dll --output-lib libmysql.a -k  (dlltool 工具是mingw內建)

  此時會產生一個libmysql.a檔案。然後把這個檔案複製到開發環境中,就基本是可以了。

  我們弄好了這些後就編譯一下,

  g++ main.cpp -o main.exe -Iinclude -L. -lws2_32 -lmysql 

  居然會出現這種錯誤

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\cc8HIzrA.o:main.cpp:(.text+0x68): undefined reference to `[email protected]4‘C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\cc8HIzrA.o:main.cpp:(.text+0xb1): undefined reference to `[email protected]32‘C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\cc8HIzrA.o:main.cpp:(.text+0xdf): undefined reference to `[email protected]8‘C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\cc8HIzrA.o:main.cpp:(.text+0x11d): undefined reference to `[email protected]4‘collect2.exe: error: ld returned 1 exit statusmake: *** [main] Error 1

  解決如下
  開啟libmysql.def修改對應出錯的地方如

  就像這樣,在後面手動增加數字,然後重複上面的操作,重新用dlltool產生一次libmysql.a連結庫

  到這裡就可以用了。

 

 

參考資料

http://www.cnblogs.com/cy163/archive/2009/10/03/1577812.html

 

mingw環境添加mysql開發庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.