TryingToTry
TryingToTry

Reputation: 1263

MySQL C++ connector unresolved external

I am trying to build a MySQL C++ connector application with Visual Studio 15 2017, using MySQL C++ connector as a static library. However, after carefully following the directions given at https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-windows-visual-studio.html, I get the LNK2001 error "unresolved external symbol _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 The above pictures show the required library settings and other settings I set according to the above link.

The code I am trying to build is:

/* 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;
}

which is from the MySQL example scripts.

Some things I have tried to fix the problem:

I have read most other posts on this topic but the solutions don't seem to work. Is there a solution for Windows?

Upvotes: 2

Views: 636

Answers (2)

TryingToTry
TryingToTry

Reputation: 1263

To use the MySQL connector/c++ on windows without building from source Visual Studio 2013 must be used with the 32 bit connector/c++ library. I had to specifically download the 32 bit library.

Upvotes: 0

Anton Malyshev
Anton Malyshev

Reputation: 8851

Put your paths in settings in quotes (because of spaces), i.e.

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

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

Also, make sure you listed all the libraries needed in Linker -> Input settings section.

Upvotes: 1

Related Questions