mysql connector cpp in centos 6 undefined reference to

梦想与她 提交于 2021-01-28 11:52:02

问题


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.


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/45325562/mysql-connector-cpp-in-centos-6-undefined-reference-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!