Ray in NY
Ray in NY

Reputation: 361

C++ / mysql Connector - undefined reference to get_driver_instance - already tried the easy stuff

Yes this question has been asked before ... I've tried everything mentioned in the previous answers. My setup is really straightforward so this shouldn't be so hard.

I just want to program against mysql using C++. My source code is taken verbatem from the 'hello world' type example here:

http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html

I am on Ubuntu 12.10. I am trying:

g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -I/usr/local/boost_1_53_0  -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18 -L/usr/lib/mysqlcppconn -lmysqlcppconn  firsttry.cpp

It compiles (if I use -c option) but won't build, giving me the infamous:

/tmp/ccn768hj.o: In function `main':
firsttry.cpp:(.text+0x3a): undefined reference to `get_driver_instance'

A few details:

Thank you in advance for any help you can provide.

Upvotes: 23

Views: 31786

Answers (4)

d3l
d3l

Reputation: 346

So I have now had this problem for a week now and I became very frustrated with it as well. I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. Here is what I have and I hope it helps.

I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with:

sudo apt-get install  libmysqlcppconn-dev

And here is my simple tester source file "tester.cpp"

#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>

using namespace sql;
int main(void){
  sql::Driver *driver;
  sql::Connection *con;

  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");
  
  return 0;
}

And finally g++ compile command:

g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn

This worked for me and I hope it helps you solve your problem!

Upvotes: 33

nakajuice
nakajuice

Reputation: 692

In case you are as forgetful as me and didn't link the library in CMakeLists.txt:

target_link_libraries(<target> mysqlcppconn)

Upvotes: 7

huangxiaowei
huangxiaowei

Reputation: 21

If all the paths are included throw param -I. You would see whether there is a problem if you compile like this:

g++ -g  -o0  -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g  -o0 -L/usr/local/lib -L/usr/local/mysql/lib -lmysqlcppconn  main.o  -o test  

the problem will appear:

main.o: In function `main':
/home/huangxw/workspace/public/soal/test/main.cpp:165: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status

Now you must adjust the order of -lmysqlcppconn and main.o:

g++ -g  -o0  -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g  -o0 -L/usr/local/lib -L/usr/local/mysql/lib main.o  -o test  -lmysqlcppconn

That is all!! The reason is simple. You can find out using the web or ask me to elaborate.

Upvotes: 2

Tarun
Tarun

Reputation: 689

For me simply swapping the order of the last two arguments fixed this problem. I don't know why but the linker is able to find the function get_driver_instance if I specify the -lmysqlcppconn option at the end after the source file.

g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -L/usr/lib/mysqlcppconn firsttry.cpp -lmysqlcppconn

Also note that I took out the following options as I think they are redundant

-I/usr/local/boost_1_53_0  -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18

Upvotes: 10

Related Questions