C/C++ 串連 MySQL (GCC 版) 以及 VC版

來源:互聯網
上載者:User
http://colorado.javaeye.com/category/48736?show_full=trueC/C++ 串連 MySQL (GCC 版)1、從 mysql-noinstall-5.1.30-win32.zip 包中解壓 include, lib 兩個目錄到
使用C:\vcprojects\mysql-5.1.30 中

2、參考本部落格《Windows XP下MinGW的安裝與配置 》建好 Windows 下 GCC 的開發環境。

3、進入目錄C:\vcprojects\mysql-5.1.30\lib\opt執行命令:
reimp -d libmysql.lib
dlltool -k -d libmysql.def -l libmysql.a

產生gcc所需連結的mysql庫 libmysql

4、在C:\vcprojects\learn目錄下建立檔案 visitsql.c (網友代碼修改版)

#include <windows.h>#include <iostream>#include <mysql.h>#include <stdio.h>using namespace std;int main(){    //connection params    //FILE *file;    char *host = "localhost";    char *user = "root";    char *pass = "12345";    char *db = "mysql";        //sock    MYSQL *sock;    MYSQL_RES *results;    MYSQL_ROW record;    sock = mysql_init(0);    if (sock)         cout << "sock handle ok!" << endl;    else     {        cout << "sock handle failed!" << mysql_error(sock) << endl;    }    //connection    if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))        cout << "connection ok!" << endl;    else     {        cout << "connection fail: " << mysql_error(sock) << endl;    }    mysql_set_character_set(sock,"gb2312");        //connection character set    cout << "connection character set: " << mysql_character_set_name(sock) << endl;    //wait for posibility to check system/mysql sockets    if(mysql_query(sock,"select * from user"))    {        cout<<"very good!"<<endl;                                }    results=mysql_store_result(sock);    printf("主機\t使用者名稱\n");    while(record = mysql_fetch_row(results))    {        printf("%s\t%s \n",record[0],record[1]);    }    mysql_free_result(results);    //system("PAUSE");        //closing connection    mysql_close(sock);    return EXIT_SUCCESS;}

5、建立一個批次檔 build.bat:
set MYSQL_CLIB=C:\vcprojects\mysql-5.1.30

set C_INCLUDE_PATH=%C_INCLUDE_PATH%;%MYSQL_CLIB%\include
set CPLUS_INCLUDE_PATH=%CPLUS_INCLUDE_PATH%;%MYSQL_CLIB%\include
set LIBRARY_PATH=%LIBRARY_PATH%;%MYSQL_CLIB%\lib\opt

g++ -o visitsql visitsql.c -llibmysql

執行該批處理,產生 visitsql.exe 執行該程式結果:
sock handle ok!
connection ok!
connection character set: gb2312
主機    使用者名稱
localhost       root

6、比較visitsql 大小:
gcc 版          488KB
vc++ debug 版   40KB
vc++ release 版 10KB

7、另從網上摘抄
如果你的dlltool產生的libmysql.a是空檔案的話,如下操作:
cd {mysql}/lib/opt
reimp libmysql.lib
dlltool.exe -d {mysql++}/libmysqlclient.def -e mysqlclient.exp -l mysqlclient.a -S as

備忘:
mingw-utils-0.3.tar.gz
binutils-2.17.50-20060824-1.tar.gz

//reimp mysqlclient.lib
//ar rcs libmysqlclient.a *.obj
//rm *.obj

我對 gcc 瞭解不多,這段不知道何意,先記下來。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.