MySQL C ++连接器未解析外部

时间:2017-12-23 21:56:28

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

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

Additional Include Directories (boost include cut-off) Linker Library Path Static Linker Library This is required for reasons described in the link 以上图片显示了我根据上述链接设置的所需库设置和其他设置。

我想要构建的代码是:

/* 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的解决方案?

2 个答案:

答案 0 :(得分:0)

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

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

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

另外,请确保列出了Linker - &gt;中所需的所有库。 Input设置部分。

答案 1 :(得分:0)

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