我遵循了本教程 http://blog.ulf-wendel.de/?p=215#hello 。我在 Visual C++ 2008 和 Visual C++ 2010 上都试过了。无论是静态的还是动态的,编译器都给了我完全相同的错误消息:
error LNK2001: unresolved external symbol _get_driver_instance
有没有人遇到过这个问题?
更新:
+ 附加依赖项:mysqlcppconn.lib
+ 附加包含目录:C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include
+ 附加库目录:C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\opt
另一个更新:在 get_driver_instance() 上单击 F12 链接到:
class CPPCONN_PUBLIC_FUNC Driver
{
protected:
virtual ~Driver() {}
public:
// Attempts to make a database connection to the given URL.
virtual Connection * connect(const std::string& hostName, const std::string& userName, const std::string& password) = 0;
virtual Connection * connect(std::map< std::string, ConnectPropertyVal > & options) = 0;
virtual int getMajorVersion() = 0;
virtual int getMinorVersion() = 0;
virtual int getPatchVersion() = 0;
virtual const std::string & getName() = 0;
};
} /* namespace sql */
extern "C"
{
CPPCONN_PUBLIC_FUNC sql::Driver *get_driver_instance();
}
显然,该函数存在,但链接器找不到它。
代码片段:
#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
using namespace std;
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
int main() {
try {
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
conn = driver->connect( "http://localhost/chandb", "root", "chan" );
stmt = conn->createStatement();
res = stmt->executeQuery( "select * from another" );
while( res->next() ) {
cout << "id = " << res->getInt( "Id" );
cout << "id = " << res->getInt( "GoldValue" );
cout << "id = " << res->getString( "Model" );
}
delete conn;
delete stmt;
delete res;
std::cout << "This is it";
}
catch( sql::SQLException e ) {
cout << e.what();
}
}
谢谢,
陈
原文由 roxrook 发布,翻译遵循 CC BY-SA 4.0 许可协议
根据 MySQL 5.1 参考手册,如果您使用的是 MySQL 连接器 C++ 的 1.1 版:
“get_driver_instance() 现在仅在动态库构建中可用 - 静态构建没有此符号。这样做是为了适应使用 LoadLibrary 或 dlopen 加载 DLL。如果您不使用 CMake 构建源代码,则需要定义mysqlcppconn_EXPORTS 如果您正在动态加载并且想要使用 get_driver_instance() 入口点。”
如果我正确理解前面的注释,您必须使用动态构建并定义
mysqlcppconn_EXPORTS
。