mysql连接器cpp在centos 6中未定义引用

时间:2017-07-26 11:25:06

标签: c++ mysql centos g++ connector

我已经安装了mysql cpp connectorBoost以及g ++编译器。

当我编写使用mysql cpp connector的程序时,它会给我错误:

  

demo.cpp :(。text + 0x3a):对'get_driver_instance'的未定义引用
    collect2:ld返回1退出状态

我用来构建此代码的命令是:

g++ demo.cpp -o demo

我的源代码是:

#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;
}

有人可以为此提出一些解决方案吗? 我已经尝试了很多东西,但它没有用。
我需要重新安装一切吗?

我已经关注了

  

MySQL连接器安装步骤

如MySQL的帮助中所述。

2 个答案:

答案 0 :(得分:1)

您当前的构建命令:g++ demo.cpp -o demo不包含应链接库的链接器ld的信息。因此,您会收到链接器错误:

  

demo.cpp :(。text + 0x3a):未定义引用&#39; get_driver_instance&#39;
  collect2:ld返回1退出状态

this documentation中编写了需要哪些库。

您可以静态链接或动态链接 静态链接意味着您的可执行文件将在没有安装所需库的计算机上运行,​​因为库位于可执行文件中。这也使得可执行文件的大小更大。对于MySQL Connector / C ++,库是:libmysqlcppconn-static.alibmysqlclient.a
动态链接意味着您的可执行文件需要在应运行它的计算机上查找库。所需的库是:libmysqlcppconn.so

使用动态链接(使用libmysqlcppconn.so)的构建命令应如下所示:

g++ demo.cpp -o demo -lmysqlcppconn

进一步注意-l-L之间的区别[{3}}或here on SO

  

-L是包含库的目录的路径。库的搜索路径。

     

-l是您要链接到的库的名称。

您不需要路径(-L),因为库应位于/usr/local/lib下,这是默认安装,并且已经位于链接器的搜索路径中。

答案 1 :(得分:0)

请尝试(使用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

并使用您的文件名更改mysql-connector路径。

相关问题