c++ - mysql connector cpp in centos 6 undefined reference to -


i installed mysql cpp connector , boost , g++ compiler.

when write program uses mysql cpp connector gives me error:

demo.cpp:(.text+0x3a): undefined reference 'get_driver_instance'
collect2: ld returned 1 exit status

the command i'm using build code is:

g++ demo.cpp -o demo 

my source code :

#include <stdlib.h> #include <iostream> #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) {    cout << endl;    cout << "running 'select 'hello world!'  _message'..." << endl;     try {       sql::driver *driver;       sql::connection *con;       sql::statement *stmt;       sql::resultset *res;        /* create connection */       driver = get_driver_instance();       con = driver->connect("tcp://127.0.0.1:3306", "root", "root");       /* connect mysql test database */       con->setschema("test");        stmt = con->createstatement();       res = stmt->executequery("select 'hello world!' _message"); // replace            statement       while (res->next()) {          cout << "\t... mysql replies: ";          /* access column data alias or column name */          cout << res->getstring("_message") << endl;          cout << "\t... mysql says again: ";          /* access column fata numeric offset, 1 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; } 

can please suggest solution this?
have tried many things didn't work.
need re-install everything?

i followed the

mysql-connector installation steps

as described in mysql's help.

your current build command: g++ demo.cpp -o demo doesn't contain informations linker ld libraries should linked against. because of linker error:

demo.cpp:(.text+0x3a): undefined reference 'get_driver_instance'
collect2: ld returned 1 exit status

in this documentation written libraries needed.

you can either link static or dynamically.
static linking means executable run on machines doesn't have needed libraries installed libraries inside executable. makes executable bigger in size. in case of mysql connector/c++ libraries are: libmysqlcppconn-static.a , libmysqlclient.a
dynamic linking means executable need find libraries on machine should run. needed library is: libmysqlcppconn.so.

your build command dynamic linking (using libmysqlcppconn.so) should like:

g++ demo.cpp -o demo -lmysqlcppconn 

further note difference between -l , -l mentioned here on so or here in official gcc linker documentation:

-l path directories containing libraries. search path libraries.

-l name of library want link to.

you dont need path (-l) here libraries should lie under /usr/local/lib default installation , in search path of linker.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -