2

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:

  • Change the project platform from Win32 to x64 and solution platform from x86 to x64 (Caused error LNK2038 "mismatch detected for '_MSC_VER': value '1800' doesn't match value '1900'")
  • Type full path name for library
  • Build connector/C++ from source (did not work, but not sure if I did it correctly).

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

TryingToTry
  • 1,033
  • 2
  • 9
  • 9
  • Possible duplicate of [C++ / mysql Connector - undefined reference to get\_driver\_instance - already tried the easy stuff](https://stackoverflow.com/questions/15995319/c-mysql-connector-undefined-reference-to-get-driver-instance-already-tri) – SoronelHaetir Dec 23 '17 at 22:00
  • No, that post is about linux – Anton Malyshev Dec 23 '17 at 22:12

2 Answers2

1

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.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
  • Thank you, I have gone ahead and done that. It did not change the error, but it is good to know. I believe I have every library required linked (only one), as that is the only one mentioned in the MySQL docs. – TryingToTry Dec 23 '17 at 22:42
  • Also you can search for the text "_get_driver_instance" inside library files. `find "_get_driver_instance" *.lib` would work in command line – Anton Malyshev Dec 23 '17 at 22:58
  • The commands brings up "sql_mysql_get_driver_instance" from mysqlcppconn-static.lib. – TryingToTry Dec 23 '17 at 23:48
0

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.

TryingToTry
  • 1,033
  • 2
  • 9
  • 9