我正在尝试编译一段简单的代码,该代码使用 centos 6.3 上的 libpqxx 库从数据库中选择多行。
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[])
{
char * sql;
try{
connection C("dbname=testdb user=postgres password=cohondob \
hostaddr=127.0.0.1 port=5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
/* Create SQL statement */
sql = "SELECT * from COMPANY";
/* Create a non-transactional object. */
nontransaction N(C);
/* Execute SQL query */
result R( N.exec( sql ));
/* List down all the records */
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
cout << "ID = " << c[0].as<int>() << endl;
cout << "Name = " << c[1].as<string>() << endl;
cout << "Age = " << c[2].as<int>() << endl;
cout << "Address = " << c[3].as<string>() << endl;
cout << "Salary = " << c[4].as<float>() << endl;
}
cout << "Operation done successfully" << endl;
C.disconnect ();
}catch (const std::exception &e){
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
我收到以下链接器错误:
ASP:/root/test/libpqxx_folder$ g++ -g libpqxx_select_from_table.cpp -L/usr/lib64 -lpqxx -lpq -o 选择 libpqxx_select_from_table.cpp: 在函数'int main(int, char**)'中: libpqxx_select_from_table.cpp: 21:警告:不推荐将字符串常量转换为“char*”/tmp/cc0bFRAY.o:在函数
main': /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:30: undefined reference to
pqxx::result::begin() const'/root/test/libpqxx_folder/libpqxx_select_from_table.cpp:31:未定义引用pqxx::tuple::operator[](int) const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:32: undefined reference to
pqxx::tuple::operator const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:33: 未定义引用pqxx::tuple::operator[](int) const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:34: undefined reference to
pqxx::tuple::operator const' /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:35: 未定义引用pqxx::tuple::operator[](int) const' /tmp/cc0bFRAY.o: In function
bool pqxx::field::to, std::allocator > >(std::basic_string, std::allocator >&) const': /usr/local/include/pqxx/field.hxx:190: 未定义对pqxx::field::c_str() const' /usr/local/include/pqxx/field.hxx:191: undefined reference to
pqxx的引用::field::is_null() const' /usr/local/include/pqxx/field.hxx:192: 未定义对pqxx::field::size() const' /tmp/cc0bFRAY.o: In function
const_result_iterator'的引用': /usr/local/include/pqxx/result.hxx:334: 未定义对pqxx::tuple::tuple(pqxx::result const*, unsigned long)' /tmp/cc0bFRAY.o: In function
bool的引用pqxx::field::to(int&) const': /usr/local/include/pqxx/field.hxx:119: 未定义引用pqxx::field::c_str() const' /usr/local/include/pqxx/field.hxx:120: undefined reference to
pqxx::field::is_null() const' /tmp/cc0bFRAY.o: 在函数bool pqxx::field::to<float>(float&) const': /usr/local/include/pqxx/field.hxx:119: undefined reference to
pqxx::field::c_str() const' /usr/local/include/pqxx/field.hxx:120: 未定义引用 `pqxx::field::is_null() const' collect2: ld 返回 1 个退出状态
在此先感谢您的帮助!