如何修复由MySql Connector C++引起的未解析的外部符号?

Cha*_*han 9 c++ mysql

我按照本教程http://blog.ulf-wendel.de/?p=215#hello.我在Visual C++ 2008和Visual C++ 2010上都尝试过.无论是静态还是动态,编译器都给了我相同的确切错误消息:

error LNK2001: unresolved external symbol _get_driver_instance
Run Code Online (Sandbox Code Playgroud)

有没有人以前遇到过这个问题?

更新:
+其他依赖项:mysqlcppconn.lib
+其他包含目录:C:\ Program Files\MySQL\MySQL Connector C++ 1.0.5\include
+其他库目录:C:\ Program Files\MySQL\MySQL Connector C++ 1.0.5\LIB \选择

另一个更新:点击链接到的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();
}
Run Code Online (Sandbox Code Playgroud)

显然,该功能存在,但链接器找不到它.

代码段:

#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();
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢,

Lui*_* R. 5

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

  • 非常感谢您的快速回复。但是,我做到了,它仍然产生相同的确切错误:(! (3认同)

wim*_*ica 4

如果使用静态链接,则需要CPPCONN_PUBLIC_FUNC=设置Project / Properties / C++ / Preprocessor / Preprocessor Definitions