程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> vc++2013中使用MySQL connector/C++ 1.1.4靜態鏈接報錯,20131.1.4

vc++2013中使用MySQL connector/C++ 1.1.4靜態鏈接報錯,20131.1.4

編輯:C++入門知識

vc++2013中使用MySQL connector/C++ 1.1.4靜態鏈接報錯,20131.1.4


 包含頭文件

#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)" (__imp_?get_driver_instance@mysql@sql@@YAPAVMySQL_Driver@12@XZ)
1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: virtual __thiscall sql::SQLException::~SQLException(void)" (__imp_??1SQLException@sql@@UAE@XZ)
1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@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 " (__imp_?getSQLState@SQLException@sql@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: char const * __thiscall sql::SQLString::c_str(void)const " (__imp_?c_str@SQLString@sql@@QBEPBDXZ)
1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??0SQLString@sql@@QAE@QBD@Z)
1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QAE@XZ)

 

  

通過觀察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

 


使用MySQL ODBC Connector連接mysql數據庫正常,但用asp代碼鏈接就報錯,什問題?

mysql一般是結合php用的..asp 可以用mssql

當然asp也可以用mysql,,先要建立系統DSN
strConnection = "dsn=xxx;driver={myodbd driver};server=localhost;uid=root;pwd=;database=xxx"
Set adoDataConn = Server.CreateObject("ADODB.Connection")
adoDataConn.Open strConnection
 

今天編譯mysql-connector-c++-113遇到點問題,看看小弟先您了

在-DBOOST_ROOT變量前或後加上-D MYSQL_DIR="D:\mysql-5.6.10-win32" -D MYSQL_INCLUDE_DIR="D:\mysql-5.6.10-win32\include" 確保變量被引用
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved