vc++2013中使用MySQL connector/C++ 1.1.4靜態連結報錯

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   使用   for   

 包含標頭檔

#include <mysql_connection.h>#include <mysql_driver.h>#include <cppconn/statement.h>#include <cppconn/resultset.h>#include <cppconn/exception.h>#ifdef _DEBUG#pragma comment(lib, "mysqlcppconn.lib")#else#pragma comment(lib, "mysqlcppconn-static.lib")// 我的MySQL connector/C++是自己下源碼編譯的,需要引入這個,官方直接提供的二進位我不清楚需要不需要#pragma comment(lib, "mysqlclient.lib")#endif

代碼

try    {        const char* user = "root";        const char* passwd = "123456";        const char* host = "tcp://192.168.1.8:3306";        const char* database = "mysql";        sql::mysql::MySQL_Driver* driver = sql::mysql::get_driver_instance();        sql::Connection* conn = driver->connect(host, user, passwd);        conn->setSchema(database);        sql::Statement *stmt = conn->createStatement();        sql::ResultSet *res = stmt->executeQuery("select * from user;");        while (res->next()) {            AfxMessageBox((res->getString(2) + " | " + res->getString(3)).c_str());        }        delete res;        delete stmt;        delete conn;    }    catch (sql::SQLException e) {        CString strErrorMsg;        strErrorMsg.Format("MySQL error code %d: %s, %s", e.getErrorCode(), e.what(), e.getSQLState().c_str());        AfxMessageBox(strErrorMsg);        if (e.getErrorCode() == 1047) {            AfxMessageBox("1047");        }    }    catch (std::runtime_error e) {        AfxMessageBox(e.what());    }

靜態連結

1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) class sql::mysql::MySQL_Driver * __cdecl sql::mysql::get_driver_instance(void)" ([email protected]@[email protected]@[email protected]12@XZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: virtual __thiscall sql::SQLException::~SQLException(void)" (__imp_??[email protected]@@[email protected])1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?[email protected]@[email protected]@QBEHXZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall sql::SQLException::getSQLState(void)const " ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@2@@[email protected]@XZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: char const * __thiscall sql::SQLString::c_str(void)const " (__imp_?[email protected]@[email protected]@QBEPBDXZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??[email protected]@@[email protected]@Z)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" ([email protected]@@[email protected])

 

  

通過觀察cppconn/build_config.h得知若要得到非__declspec(dllimport)方式的引入需要定義宏CPPCONN_LIB_BUILD

解決方案

在包含標頭檔前定義宏CPPCONN_LIB_BUILD,告訴連結器MySQL connector是靜態庫方式編譯的即可

#ifndef _DEBUG#define CPPCONN_LIB_BUILD#endif#include <mysql_connection.h>#include <mysql_driver.h>#include <cppconn/statement.h>#include <cppconn/resultset.h>#include <cppconn/exception.h>#ifdef _DEBUG#pragma comment(lib, "mysqlcppconn.lib")#else#pragma comment(lib, "mysqlcppconn-static.lib")// 我的MySQL connector/C++是自己下源碼編譯的,需要引入這個,官方直接提供的二進位我不清楚需要不需要#pragma comment(lib, "mysqlclient.lib")#endif

 

vc++2013中使用MySQL connector/C++ 1.1.4靜態連結報錯

聯繫我們

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