Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

mysql

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

like image 915
Chan Avatar asked Dec 17 '10 11:12

Chan


1 Answers

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.

like image 69
Luis G. Costantini R. Avatar answered Oct 20 '22 12:10

Luis G. Costantini R.