roxrook
roxrook

Reputation: 13853

How to fix unresolved external symbol due to MySql Connector C++?

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

Upvotes: 9

Views: 17542

Answers (10)

Brandon
Brandon

Reputation: 1421

As mentioned in previous comments the issue has to do with trying to find a dynamic configuration vs the static configuration. It also has to do with the 32-bit vs 64-bit. I had the same issue, and by reading through all the comments I figured out a solution.

First off, you should be using the latest version, which for me was 8.0.33 32-bit.

mysql-connector-c++-8.0.33-win32

In the project properties -> VC++ Directories, you'll need to include the directory of the mysql-connector-c++-8.0.33-win32 includes.

Include Directories: <...>;C:<full path>\mysql-connector-c++-8.0.33-win32\include;

Now in the settings for the Linker, you'll need to have two changes here.

Additional Library Directories: C:<full path>\mysql-connector-c++-8.0.33-win32\lib\vs14

Finally, you just have to add the correct .lib file, this being the one that isn't static.

Additional Dependencies: <...>;mysqlcppconn.lib;

One special note that I found while doing implementation is that I had to work with the device/connection within a .cpp file and I couldn't specify my code in a .h file. I think it had to do with being in the context of a class, and not statically referenced.

Thus, in my .cpp file, at the top I used

#include <mysql/jdbc.h>

And then I had access to everything I needed. You can test all your code out in the int main() and refactor afterwards.

Upvotes: 0

Marodheure
Marodheure

Reputation: 3

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:

The goal is to compile mysql cpp connector on your computer, to have lib which work on your computer when you compile somewhat with.

DEPENDENCIES:

  1. clone (recursively with submodules, to have the folder jdbc) the repo on master branch (same than 8.0.27 for me when i write this post) of mysql cpp connector source code for windows on github:

  2. 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 put mysql-8.0.27-winx64 in C:\Users\< USER >\Downloads\mysql-8.0.27-winx64

  3. download openssl (Win64 OpenSSL v1.1.1m):

  4. download boost: https://www.boost.org/users/download/

    • untarGz and put it here: E:\boost_1_78_0

CONFIGURATION:

for the configuration you must have this folder architecture:

. Parent Folder (for me it's E:\MySql)
|
|\
| \
|  . _SOURCE (renamed mysql cpp connector repo)
|  |
|  .
|  .
|  .
|  |
|  |\
|  | \
|  |  . jdbc (jdbc submodule)
|  .
|  .
|  .
|
|\
| \
|  . _BUILD
 \
  \
   . _INSTALL

with this folder architecture if you have errors in next commands, your sources will not to be modify, you will have just to remove the containt of _BUILD and _INSTALL folders to relaunch

  • open powershell in E:\MySql and launch cmd:

cmake .\_SOURCE\ -B .\_BUILD\ -D CMAKE_INSTALL_PREFIX=.\_INSTALL -D CMAKE_BUILD_TYPE=Debug -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"

  • the last lines of output must to be like:

-- Configuring done

-- Generating done

-- Build files have been written to: Your-Build-Path/_BUILD

BUILDING:

launch cmd: cmake --build .\_BUILD\

There are a lot of fatal errors possible:

  • mysql not found
  • boost not found
  • openssl not found
  • openssl bad version (need 1.1.1)
  • no compatible options values between options
  • forgoten options, if this is the case, try to add the option with his default value

so check yours paths dependecies if you have errors

There somes not fatal warning:

  • the "is_same" test doesn't work
  • file sys/endiand.h not found
  • file sys/byteorder.h not found

when the output say "look log files output.log and errors.log" you have fatal error some where, but may be (MAY BE) don't loss time on the warnings and on the logs files... and look all the lines of the ouput, because for me the fatals errors was not listed in the logs files but in the output (may be not for you, i don't know)

INSTALLATION:

cmake --install .\_BUILD\ --config Debug

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

Upvotes: 0

ngamc
ngamc

Reputation: 21

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).

Upvotes: 0

Raj
Raj

Reputation: 41

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/

Upvotes: 2

Roberto
Roberto

Reputation: 9

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.

Upvotes: 0

AJG85
AJG85

Reputation: 16217

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.

Upvotes: 1

Luis G. Costantini R.
Luis G. Costantini R.

Reputation: 1128

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.

Upvotes: 5

engf-010
engf-010

Reputation: 3929

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 !

Upvotes: 1

greatwolf
greatwolf

Reputation: 20878

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?

Upvotes: 1

wimh
wimh

Reputation: 15232

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

Upvotes: 4

Related Questions