MySQL C ++ Connector: undefined reference to `get_driver_instance '

I am trying to get work with MySQL connector. I installed both the connector and the mysql client library, but I still get this error:

obj/Database.obj: In function `Database::connect()': /home/xeross/alpine/src/server/Database.cpp:13: undefined reference to `get_driver_instance' collect2: ld returned 1 exit status make[2]: *** [alpine-server] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2 

Using Ubuntu 10.04 And my makefile looks like this:

 INCLUDES = -I./src -I./src/shared OUTDIR = bin INTDIR = obj OPTIONS = -ggdb -g3 -Wall -O0 alpine-server : Shared.a AsyncServerSocket.obj PlayerHandler.obj PacketHandler.obj Session.obj User.obj Database.obj init g++ $(INCLUDES) $(OPTIONS) -static \ -pthread \ -lmysqlcppconn-static \ -o $(OUTDIR)/alpine-server src/server/main.cpp \ $(INTDIR)/AsyncServerSocket.obj \ $(INTDIR)/PacketHandler.obj \ $(INTDIR)/Database.obj \ $(INTDIR)/PlayerHandler.obj \ $(INTDIR)/Session.obj \ $(INTDIR)/User.obj \ $(INTDIR)/Shared.a \ -lboost_system \ -lmysqlclient AsyncServerSocket.obj : src/server/AsyncServerSocket.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/AsyncServerSocket.obj src/server/AsyncServerSocket.cpp PlayerHandler.obj : src/server/PlayerHandler.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/PlayerHandler.obj src/server/PlayerHandler.cpp PacketHandler.obj : src/server/PacketHandler.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/PacketHandler.obj src/server/PacketHandler.cpp Session.obj : src/server/Session.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Session.obj src/server/Session.cpp User.obj : src/server/User.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/User.obj src/server/User.cpp Database.obj : src/server/Database.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Database.obj src/server/Database.cpp # Shared.a Shared.a : Packet.obj Flags.obj AsyncSocket.obj Log.obj init ar -cvq $(INTDIR)/Shared.a \ $(INTDIR)/Packet.obj \ $(INTDIR)/Flags.obj \ $(INTDIR)/AsyncSocket.obj \ $(INTDIR)/Log.obj ranlib $(INTDIR)/Shared.a Packet.obj : src/shared/packet.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Packet.obj src/shared/packet.cpp Flags.obj : src/shared/Flags.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Flags.obj src/shared/Flags.cpp AsyncSocket.obj : src/shared/AsyncSocket.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/AsyncSocket.obj src/shared/AsyncSocket.cpp Log.obj : src/shared/Log.cpp init g++ -c $(INCLUDES) $(OPTIONS) -o $(INTDIR)/Log.obj src/shared/Log.cpp init: mkdir -p bin obj clean: rm -f $(INTDIR)/*.obj $(INTDIR)/*.a 

The code

 // Excerpt from .hpp file #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/resultset.h> #include <cppconn/statement.h> // End excerpt void Database::connect() { std::stringstream connString; connString << "tcp://"; connString << m_host; connString << ":"; connString << m_port; m_driver = get_driver_instance(); // This guy is being a ***** m_conn = m_driver->connect(connString.str(), m_user, m_password); m_conn->setSchema(m_database); } 

What can I do to fix this?

+6
c ++ mysql mysql-connector makefile
source share
5 answers

Finally, I was able to successfully compile the program with a C ++ connector in Ubuntu 10.10.

Initially, I ran into the same problem as the "undefined link to get_driver_instance '" to solve this problem. I declare my driver instance variable of type MySQL_Driver. For help, this type is defined in mysql_driver.h. Here is a piece of code that I used in my program.

 sql::mysql::MySQL_Driver *driver; try { driver = sql::mysql::get_driver_instance(); } 

and I compiled the program using the -l option mysqlcppconn linker

+8
source share

Thank you so much, I fixed it too. I had the exact experience.

I use Eclipse CDT on 64-bit CentOS, and for those who read this, the following steps.

  • First, make sure the following is indicated in the code.

include "mysql_driver.h"

include "mysql_connection.h"

using namespace sql::mysql;

  1. Make sure you specify in Eclipse in the Eclipse project settings. the mysql/include and mysql/include/cppconn in your include, and then also mysql/lib in the library directory, and more importantly, you specify -lmysqlcppconn .

  2. Make sure you also set -m64 in the Eclipse compiler options.

  3. When you run your program, it may complain about the lack of libmysqlcppconn.so.1 . Do this, copy libmysqlcppconn.so.1.0.5 to your /usr/lib64 . Link libmysqlcppconn.so.1 towards libmysqlcppconn.so.1.0.5 inside this directory.

Your program should now work.

+3
source share

The code would be more useful than the make file, but try adding using namespace sql; to the beginning of Database.cpp.

 // Excerpt from .hpp file #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/resultset.h> #include <cppconn/statement.h> using namespace sql; // <---- add here 
+2
source share

You need to add -lmysqlcppconn-static after the object files that use stuff inside this library.

+2
source share

You need to associate with

 -lmysqlcppconn -lmysqlcppconn-static 

The first library contains the code for the headers in / usr / include / cppconn / , and the second library contains the code found in the headers mysql_driver.h and mysql_connection.h .

+1
source share

All Articles