【问题标题】:MySQL C++ connector unresolved externalMySQL C++ 连接器未解析的外部
【发布时间】:2017-12-23 21:56:27
【问题描述】:

我正在尝试使用 Visual Studio 15 2017 构建 MySQL C++ 连接器应用程序,使用 MySQL C++ 连接器作为静态库。但是,在仔细按照https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-windows-visual-studio.html 给出的指示后,我得到了 LNK2001 错误“未解析的外部符号 _get_driver_instance”。

上面的图片显示了我根据上面的链接设置的所需的库设置和其他设置。

我正在尝试构建的代码是:

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main(void)
{
    try {
    sql::Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;
    sql::ResultSet *res;

    /* Create a connection */
    driver = get_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
    /* Connect to the MySQL test database */
    con->setSchema("test");

    stmt = con->createStatement();
    res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
    while (res->next()) {
        cout << "\t... MySQL replies: ";
        /* Access column data by alias or column name */
        cout << res->getString("_message") << endl;
        cout << "\t... MySQL says it again: ";
        /* Access column data by numeric offset, 1 is the first column */
        cout << res->getString(1) << endl;
    }
    delete res;
    delete stmt;
    delete con;

    }
    catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " 
            << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

    cout << endl;

    return EXIT_SUCCESS;
}

来自 MySQL 示例脚本。

我尝试解决的一些问题:

  • 将项目平台从 Win32 更改为 x64,将解决方案平台从 x86 更改为 x64(导致错误 LNK2038“检测到 '_MSC_VER' 不匹配:值 '1800' 与值 '1900' 不匹配”)
  • 输入库的完整路径名
  • 从源代码构建连接器/C++(不起作用,但不确定我是否正确)。

我已阅读有关此主题的大多数其他帖子,但解决方案似乎不起作用。有适用于 Windows 的解决方案吗?

【问题讨论】:

标签: c++ mysql visual-studio-2017


【解决方案1】:

将您的路径放在引号中的设置中(因为空格),即

"C:\Program Files\MySQL\Connector C++ 1.1\lib\opt";

"C:\Program Files\MySQL\Connector C++ 1.1\include";...

另外,请确保您在 Linker -> Input 设置部分列出了所有需要的库。

【讨论】:

  • 谢谢,我已经完成了。它没有改变错误,但很高兴知道。我相信我已经链接了每个所需的库(只有一个),因为这是 MySQL 文档中唯一提到的一个。
  • 您也可以在库文件中搜索文本“_get_driver_instance”。 find "_get_driver_instance" *.lib 可以在命令行中工作
  • 命令从 mysqlcppconn-static.lib 中调出“sql_mysql_get_driver_instance”。
【解决方案2】:

要在 Windows 上使用 MySQL 连接器/c++ 而不从源代码构建,Visual Studio 2013 必须与 32 位连接器/c++ 库一起使用。我必须专门下载 32 位库。

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多