标签: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)" (__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
vc++2013中使用MySQL connector/C++ 1.1.4静态链接报错
标签:style blog http io color os ar 使用 for
原文地址:http://www.cnblogs.com/sankeyou/p/4054472.html