Linux下C++/C串連MySQL資料庫

來源:互聯網
上載者:User

標籤:拷貝、編譯、執行

    虛擬機器的版本:redhat7.0  64bit

    資料庫的版本:MySQL5.7      


    1、將解壓得到的檔案裡對應的庫,標頭檔拷貝到系統能找到的位置

        例如:我當時安裝資料庫的檔案都在:/usr/local/下

    執行:cp /usr/local/include/* /usr/include/

    注意,拷貝時,如果*裡含有檔案夾,則需要用:

          cp -r /usr/local/include/* /usr/include/ 

        (cp將遞迴複製該目錄下所有的子目錄和檔案)

    隨意寫一段測試檔案:1.cpp

    

    # include <iostream>

    # include <mysql/mysql.h>


    using namespace std;


    int main()

    {

        MYSQL * connection;

        connection = mysql_init(NULL);


        return 0;

    }

    

    然後用 g++ -o 1 1.cpp -lmysqlcient 編譯檔案

    很不幸,又是意料之中的出現了一個問題:

        650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/89/43/wKioL1gOA-nRQRJWAAAd1p3jhe8107.png-wh_500x0-wm_3-wmp_4-s_1009283184.png" title="QQ20161024204810.png" alt="wKioL1gOA-nRQRJWAAAd1p3jhe8107.png-wh_50" />   

    找不到庫檔案導致的。

    終端輸入命令:mysql_config --libs

        650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/89/45/wKiom1gOBYTACaqoAAAX3czMFw0193.png-wh_500x0-wm_3-wmp_4-s_3707091375.png" title="QQ20161024204935.png" alt="wKiom1gOBYTACaqoAAAX3czMFw0193.png-wh_50" />

    現在我們編譯時間加上路徑,執行:g++ -L/usr/local/lib/mysql -lmysqlclient -o 1 1.cpp

編譯通過。

    連結時預設路徑為 /usr/local/lib,多了一層目錄,我們將mysql下的檔案拷貝到lib下

    執行:cp /usr/local/lib/mysql/* /usr/local/lib/

    此時刪不刪lib下的mysql檔案夾都無礙了。

    再次執行 g++ -o 1 1.cpp -lmysqlclient 編譯通過。650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0058.gif" alt="j_0058.gif" />

     

         

本文出自 “11561636” 部落格,請務必保留此出處http://11571636.blog.51cto.com/11561636/1865184

Linux下C++/C串連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.