9
votes

I followed this tutorial http://blog.ulf-wendel.de/?p=215#hello. I tried both on Visual C++ 2008 and Visual C++ 2010. Either static or dynamic, the compiler gave me the same exact error messages:

error LNK2001: unresolved external symbol _get_driver_instance

Has anyone experience this issue before?

Update:
+ Additional Dependencies: mysqlcppconn.lib
+ Additional Include Directories: C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include
+ Additional Libraries Directories: C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\opt

Another Update: Click F12 on get_driver_instance() linked to:

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();
}

Apparently, the function existed, but the linker could not find it.

Code snippet:

#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();
    }
}

Thanks,
Chan

9
How are you linking your program?unquiet mind
Probably you don't add some libs when linking.Pawel Zubrycki
I followed the tutorial above at least 3 times. I checked very carefully in both Debug and Release mode. I really couldn't find where I missed :(Chan
Are you trying to do both dynamic as well as static linking ? You should be doing only one. The tutorial shows to do both and you probably have done it twice.DumbCoder
I did once and then create another project for another. Thanks!Chan

9 Answers

5
votes

According to MySQL 5.1 Reference Manual if you are using the Version 1.1 of the MySQL Connector C++:
"get_driver_instance() is now only available in dynamic library builds - static builds do not have this symbol. This was done to accommodate loading the DLL with LoadLibrary or dlopen. If you do not use CMake for building the source code you will need to define mysqlcppconn_EXPORTS if you are loading dynamically and want to use the get_driver_instance() entry point."
If I understand the previous note correctly, you have to use the dynamic build and define mysqlcppconn_EXPORTS.

3
votes

If you use static linking, you need to set CPPCONN_PUBLIC_FUNC= in Project / Properties / C++ / Preprocessor / Preprocessor Definitions

2
votes

I faced Same Error while using c++/Sql Connector. and after spending entire day I solved this problem by using 32 bit libmysql.dll , 32 bit libmysqlcppcon.lib and 32 bit libmysqlcppcon.dll ( Dynamic binding is used because driver instance creation is only available in dynamic library of libmysqlcppcon.lib)

I found very good link explaining everything along with the working code. I think it would help others if they stuck at the same issue.

http://r3dux.org/2010/11/how-to-use-mysql-connectorc-to-connect-to-a-mysql-database-in-windows/

1
votes

error LNK2001: unresolved external symbol _get_driver_instance

That error message basically means that the linker cannot locate the function get_driver_instance anywhere, eg. it's not in any of the object files from your compilation units, not in a static .lib library or import library. This can also happen if the mangled names in the library doesn't match exactly with what the linker expects to find. eg. Library was built with different calling convention etc.

I would suggest that you take a look at the final command line being used from your project and update your post here for both compiling and linking. Second, make sure the mysql library you're linking against has that function. I believe there's a commandline tool in vs for dumping and viewing the contents of a static lib.

If you're linking against a dynamic dll version of mysql, make sure the import library contains the missing referenced symbol. Additionally, you can crack the dll open with a PE viewer and look under it's export tables to verify that that function actually exist and is visible.

Edit: According to this post, the function get_driver_instance should be in 'mysqlcppconn.lib' somewhere. Are you sure that file is being properly linked?

1
votes

I read the article you mentioned. It has this line for static compilation : As said, you need to link both "mysqlcppconn-static.lib" and "libmysql.lib".

Since you don't mention this is you question ,have you tried this ?

Also , on a forum I read this (for eclips) "You must have this in source":

#include "mysql_driver.h" 
#include "mysql_connection.h"
using namespace sql::mysql; 

Anyway ,good luck !

1
votes

I don't know if you have your heart set on using this MySQL Connector/Driver package but there are many robust database wrappers available for free or nearly free out there if this is causing seemingly unspoken headaches for you.

I actually was thoroughly impressed by QT database objects when I used them previously for a data model project. The QT framework is licensed under LGPL and also has a commercial version available and is a very versatile set of cross platform libraries. The QtSql objects provide a consistent interface to many databases including MySql, SQLServer, Postgres, ODBC, etc with the ability to substitute your own drivers and still use the same interface for everything at a high level in C++

Some other notable options:

SQLApi++

MySQL++

Not trying to come on like a salesman or anything and I have no affiliation with any of these but sometimes it never hurts to shop around and find something that works better for your needs.

0
votes

I solved using the 32bit version both for mysql server and connector/c++ on 64bit OS. I think the problem are the boost libraries but im not sure.

0
votes

I spent several hours trying to solve the problem exactly the same as you. I finally found out the problem myself. I downloaded the 64-bit version of MySQL/SQL Connector and followed a tutorial in MySQL.com in setting up win32 console project its properties and got the error message as mentioned. It was because the tutorial is teaching to build a 32-bit program. After I change to build a release version of X64 code, problem was solved. Make sure your library is 32bit or 64bit and then configure your Visual Studio accordingly (in configuration manager).

0
votes

Hello same problem for me, i have search and found my solution, i give you my paths on my local computer, but you can put all where you want, it's just my personnal notes:

DEPENDENCIES:

download mysql cpp connector source code for windows: mysql-connector-c++-8.0.27-src.zip: https://dev.mysql.com/downloads/connector/cpp/

download mysql server for windows: choice: Windows (x86, 64-bit), ZIP Archive version: 8.0.27 size: 209.4M url: https://dev.mysql.com/downloads/mysql/ filename: mysql-8.0.27-winx64 and just put mysql-8.0.27-winx64 in C:\Users<USER>\Downloads\mysql-8.0.27-winx64

download openssl (Win64 OpenSSL v1.1.1m) binary installer msi: https://slproweb.com/products/Win32OpenSSL.html (not official :/ but that work (and it linked by this site too: https://wiki.openssl.org/index.php/Binaries), and i have failed and give up to build from source code) Install openssl here: E:\OpenSSL-Win64 unfortunaly the version 3.0.1 doesn't work for build cpp connector (or my bad ?)

download boost: https://www.boost.org/users/download/ and untarGz and put it here: E:\boost_1_78_0

CONFIGURATION:

  • Create dir: E:\MySql

  • unzip mysql-connector-c++-8.0.27-src.zip in E:\MySql

  • Go in E:\MySql

  • Rename mysql-connector-c++-8.0.27-src by _SOURCE

  • Create dir: E:\MySql_BUILD

  • Create dir: E:\MySql_INSTALL

  • Open powershell here: E:\MySql

  • launch cmd:

cmake ._SOURCE\ -B ._BUILD\ -D CMAKE_INSTALL_PREFIX=._INSTALL -D CMAKE_BUILD_TYPE=Release -D WITH_SSL="E:\OpenSSL-Win64" -D BUILD_STATIC=OFF -D WITH_JDBC=ON -D WITH_MYSQL="C:\Users<USER>\Downloads\mysql-8.0.27-winx64" -D WITH_BOOST="E:\boost_1_78_0" -D MYSQLCLIENT_STATIC_BINDING=OFF -D MYSQLCLIENT_STATIC_LINKING=OFF -G "Visual Studio 16" -A "x64"

BUILDING:

launch cmd: cmake --build ._BUILD\

INSTALLATION:

if you launch the cmake install cmd now, you will have erros because the option "-D CMAKE_BUILD_TYPE=Release" is set on Release and unfortunaly the first cmake cmd create "Debug" folder instead "Release" so rename folders:

  • E:\MySql_BUILD\Debug by E:\MySql_BUILD\Release
  • E:\MySql_BUILD\jdbc\Debug by E:\MySql_BUILD\jdbc\Release

launch cmd: cmake --install ._BUILD\

END:

in folder: E:\MYSQL_INSTALL\lib64 there are dynamic lib

in folder: E:\MYSQL_INSTALL\lib64\vs14 there are static lib

and E:\MYSQL_INSTALL\lib64\vs14\mysqlcppconn.lib will containt "get_driver_instance" functions to use mysql cpp connector etc... if you want open mysqlcppconn.lib with 7zip, right click, open archive, open 1.txt and 2.txt, and CTRL+F your functions

NOTES:

you can't configurate in Debug (-D CMAKE_BUILD_TYPE=Release), else the first cmake cmd will create a visual studio project (E:\MYSQL_BUILD\jdbc\connector-jdbc-deps.vcxproj) with inside a unknow linked library (MYSQLLIB-NOTFOUND-DEBUG), and the cmake build cmd will fail

in my visual studio project, i have link the two static lib, and i have copy past the two dynamic lib next to my exe, that work for me

VERSION-ARCH:

software version arch
windows pro 10 x64
cmake 3.22.1 x64
Microsoft Visual Studio Community 2019 16.11.8 x64
Microsoft .NET Framework 4.8.04084 x64
openssl v1.1.1m x64
boost 1.78.0 cross-arch ?
mysql server 8.0.27 x64
mysql cpp connector 8.0.27 x64