2017-07-26 57 views
1

我已經安裝了mysql cpp connectorBoost以及g ++編譯器。mysql連接器cpp in centos 6 undefined參考

當我寫一個使用它給我的錯誤的mysql cpp connector的程序:

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

我使用的是建立這個代碼的命令是:

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-connector installation steps

在MySQL的幫助說明。

+0

看起來像一個鏈接器問題。'要使用靜態連接器/ C++庫,請鏈接兩個庫文件libmysqlcppconn-static.a和libmysqlclient.a. [source here](https://dev.mysql.com/doc/connector-cpp/en/連接器-CPP-APPS-Linux的netbeans.html)。或者動態庫:'要使用Connector/C++動態庫,將項目與單個庫文件libmysqlcppconn.so.# –

+0

已經嘗試過'mysql_driver.h'頭文件,但是它在get_driver_instance()方法調用。 – VasaraBharat

+0

問題不在於標題。這是一個鏈接器問題,所以鏈接所需的庫。 –

回答

1

您當前的編譯命令:g++ demo.cpp -o demo不包含鏈接器ld的鏈接信息。因爲你得到一個鏈接器錯誤:

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

this documentation被寫入哪些庫是需要的。

您可以鏈接靜態或動態鏈接。
靜態鏈接意味着您的可執行文件將在沒有安裝所需庫的計算機上運行,​​因爲這些庫位於可執行文件內。這也使得可執行文件變得更大。在MySQL連接器/ C++的情況下,這些庫是:libmysqlcppconn-static.alibmysqlclient.a
動態鏈接意味着您的可執行文件需要在它應該運行的機器上找到這些庫。所需的庫是:libmysqlcppconn.so

動態鏈接(使用libmysqlcppconn.so)應該像你的build命令:

g++ demo.cpp -o demo -lmysqlcppconn 

還應注意到-l-L之間的差異所提到here on SOhere 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.

你不需要一個路徑(-L),因爲這些庫應該位於/usr/local/lib這是默認安裝,並且已經在鏈接器的搜索路徑中。

+0

嗨@Vasara如果這個或任何答案已經解決了您的問題,請點擊複選標記考慮[接受它](https://meta.stackexchange.com/q/5234/179419)。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 –

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連接器的路徑與你的名和文件名。