我正在编写一个C++应用程序(使用Linux的GCC Eclipse),它应该与我的MySQL服务器进行交互。
我已经下载了MySQL连接器C++,预编译,并将文件复制到目录中(/UR/LIB,/UR/IOPE)。我在Eclipse(“MySqLCppCONN”)项目属性的GCC C++链接器部分中引用过。我的代码直接来自MySQL引用(Hello World),除了我删除了错误处理和末尾的delete语句(我甚至还没有到达那里,所以重点是什么)
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next())
{
cout << "\t... MySQL replies: ";
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
cout << res->getString(1) << endl;
}
return EXIT_SUCCESS;
}
现在,当我编译这个时,它会给我一个make error 1,手册页上说这意味着我需要重新编译我的包。我试过了,没用。
我好像到处都是死路一条。有人能告诉我怎么解决这个问题吗?(make真的可以告诉我应该重新编译什么。)有人对我哪里出错有什么建议/想法吗?
编辑-稍微修改了代码,但总的来说是一样的。
更新-如果我使用终端而不是exlipse来制作,它会告诉我,问题显然是连接器需要libstdc++.so.5,而我有libstdc++.so.6。
最佳答案
解决方案非常简单-编译自己的连接器。我用的是1.0.5版本的连接器。为此,您需要通过sudo apt-get install mysql-client
在您下载(并提取)的源包目录中,键入cmake .
显然,在驱动程序的三个文件中,引用了snprintf和printf,但没有包含stdio.h头。我补充说#include <stdio.h>
到每个文件,然后在终端中键入make
然后,我将文件复制到lib目录sudo cp path/to/driver/libmysqlcppconn* /usr/lib/
一切都很有魅力。
关于c++ - MySQL Connector C++-使错误1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1455600/