gpt4 book ai didi

c++ - 如何修复由于 MySql Connector C++ 而无法解析的外部符号?

转载 作者:可可西里 更新时间:2023-11-01 06:29:17 34 4
gpt4 key购买 nike

我遵循了本教程 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();
}
}

谢谢,

最佳答案

根据 MySQL 5.1 Reference Manual如果您使用的是 MySQL Connector C++ 1.1 版:
“get_driver_instance() 现在仅在动态库构建中可用 - 静态构建不可用有这个符号。这样做是为了适应使用 LoadLibrary 或 dlopen 加载 DLL。如果您不使用 CMake 构建源代码,如果您动态加载并希望使用 get_driver_instance() 入口点,则需要定义 mysqlcppconn_EXPORTS。”
如果我正确理解前面的注释,您必须使用动态构建并定义 mysqlcppconn_EXPORTS

关于c++ - 如何修复由于 MySql Connector C++ 而无法解析的外部符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4469899/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com