【发布时间】:2010-12-17 11:09:08
【问题描述】:
我遵循了本教程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();
}
显然,该函数存在,但链接器找不到它。
代码sn-p:
#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();
}
}
谢谢,
陈
【问题讨论】:
-
你是如何链接你的程序的?
-
可能链接的时候没有加一些库。
-
我按照上面的教程至少做了 3 次。我在调试和发布模式下都非常仔细地检查过。我真的找不到我错过的地方:(
-
您是否尝试同时进行动态和静态链接?你应该只做一个。本教程显示两者都做,你可能已经做过两次了。
-
我做了一次,然后为另一个创建了另一个项目。谢谢!