我已經安裝了mysql cpp connector
和Boost
以及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的幫助說明。
看起來像一個鏈接器問題。'要使用靜態連接器/ 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.# –
已經嘗試過'mysql_driver.h'頭文件,但是它在get_driver_instance()方法調用。 – VasaraBharat
問題不在於標題。這是一個鏈接器問題,所以鏈接所需的庫。 –