1

I already installed mysql cpp connector and Boost and also the g++ compiler.

When I write a program that uses the mysql cpp connector it gives me the error:

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

The command I'm using to build this code is:

g++ demo.cpp -o demo

My source code is :

#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!'  AS _message'..." << endl;

   try {
      sql::Driver *driver;
      sql::Connection *con;
      sql::Statement *stmt;
      sql::ResultSet *res;

      /* Create a connection */
      driver = get_driver_instance();
      con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
      /* Connect to the MySQL test database */
      con->setSchema("test");

      stmt = con->createStatement();
      res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace            with your statement
      while (res->next()) {
         cout << "\t... MySQL replies: ";
         /* Access column data by alias or column name */
         cout << res->getString("_message") << endl;
         cout << "\t... MySQL says it again: ";
         /* Access column fata by numeric offset, 1 is the 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 anyone please suggest some solution to this?
I have already tried so many things but it didn't work.
Do I need to re-install everything?

I already followed the

MySQL-connector installation steps

as described in MySQL's help.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
VasaraBharat
  • 81
  • 15
  • Looks like a linker problem. `To use the static Connector/C++ library, link against two library files, libmysqlcppconn-static.a and libmysqlclient.a.` [source here](https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-linux-netbeans.html). Or the dynamic library: `To use the Connector/C++ dynamic library, link your project with a single library file, libmysqlcppconn.so.` – Andre Kampling Jul 26 '17 at 11:28
  • already tried `mysql_driver.h` header-file but it gives conflicts in `get_driver_instance()` method call. – VasaraBharat Jul 26 '17 at 11:29
  • The problem is not about the header. It's a linker problem, so link the needed libraries. – Andre Kampling Jul 26 '17 at 11:30
  • sir i m using command line to run this code. can u suggest me how can i link library by command ? – VasaraBharat Jul 26 '17 at 11:34
  • 1
    Please edit your question and show your current `g++` command. – Andre Kampling Jul 26 '17 at 11:35
  • 1
    If you have the libraries installed this one should work: `g++ demo.cpp -o demo -lmysqlcppconn` – Andre Kampling Jul 26 '17 at 11:42
  • thank you very much sir. this command solved my issue. – VasaraBharat Jul 26 '17 at 11:48

2 Answers2

1

Your current build command: g++ demo.cpp -o demo doesn't contain informations for the linker ld which libraries should be linked against. Because of that you get a linker error:

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

In this documentation is written which libraries are needed.

You can either link static or dynamically.
Static linking means that your executable will run on machines that doesn't have the needed libraries installed as the libraries are inside the executable. This also makes the executable bigger in size. In the case of the MySQL Connector/C++ the libraries are: libmysqlcppconn-static.a and libmysqlclient.a
Dynamic linking means that your executable will need to find the libraries on the machine where it should run. The needed library is: libmysqlcppconn.so.

Your build command with dynamic linking (using libmysqlcppconn.so) should look like:

g++ demo.cpp -o demo -lmysqlcppconn

Further note the difference between -l and -L as mentioned here on SO or here in the official gcc linker documentation:

-L is the path to the directories containing the libraries. A search path for libraries.

-l is the name of the library you want to link to.

You dont need a path (-L) here as the libraries should lie under /usr/local/lib which is the default installation and is already in the search path of the linker.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • Hi @Vasara if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Andre Kampling Aug 16 '17 at 06:40
0

Please try(Generated using eclipse):

Building file: ./stack.cpp
Invoking: GCC C++ Compiler
g++ -I/opt/mysql-connector-c++-1.1.7-linux-glibc2.5-x86-64bit/include - I/opt/boost_1_61_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF "./stack.d" -MT "./stack.o" -o "./stack.o" "./stack.cpp"

Building target: mysqlExample
Invoking: GCC C++ Linker
g++ -Wl,--allow-shlib-undefined -L/opt/mysql-connector-c++-1.1.7-linux-glibc2.5-x86-64bit/lib -o "mysqlExample" ./stack.o -lmysqlcppconn

and do change mysql-connector path with your's and file names.

Harneet Singh
  • 2,328
  • 3
  • 16
  • 23