我正在尝试编译一段简单的代码,它使用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 select libpqxx_select_from_table.cpp:在函数&#39; int main(int,char **)&#39;: libpqxx_select_from_table.cpp:21:警告:不推荐将字符串常量转换为&#39; char *&#39; /tmp/cc0bFRAY.o:在函数
main': /root/test/libpqxx_folder/libpqxx_select_from_table.cpp:30: undefined reference to
pqxx :: result :: begin()const&#39; /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&#39;的未定义引用 /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&#39;的未定义引用 /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&gt; &gt;(std :: basic_string,std :: allocator&gt;&amp;)const&#39;: /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&#39; /usr/local/include/pqxx/field.hxx:192:未定义引用pqxx::field::size() const' /tmp/cc0bFRAY.o: In function
const_result_iterator&#39;: /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&amp;)const&#39;: /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&#39; /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&#39; /usr/local/include/pqxx/field.hxx:120:对`pqxx :: field :: is_null()const&#39;的未定义引用 collect2:ld返回1退出状态
提前感谢您的帮助!