我正在尝试编译libpqxx代码示例:
#include <iostream>
#include <pqxx/pqxx>
int main(int, char *argv[])
{
pqxx::connection c("dbname=company user=accounting");
pqxx::work txn(c);
pqxx::result r = txn.exec(
"SELECT id "
"FROM Employee "
"WHERE name =" + txn.quote(argv[1]));
if (r.size() != 1)
{
std::cerr
<< "Expected 1 employee with name " << argv[1] << ", "
<< "but found " << r.size() << std::endl;
return 1;
}
int employee_id = r[0][0].as<int>();
std::cout << "Updating employee #" << employee_id << std::endl;
txn.exec(
"UPDATE EMPLOYEE "
"SET salary = salary + 1 "
"WHERE id = " + txn.quote(employee_id));
txn.commit();
}使用命令:
c++ add_employee.cxx -lpqxx -lpq不幸的是,我得到了一个错误:
test2.cpp:(.text+0x162): undefined reference to `pqxx::transaction_base::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test2.cpp:(.text+0x32e): undefined reference to `pqxx::transaction_base::exec(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cchvWstf.o: In function `pqxx::string_traits<int>::null()':
test2.cpp:(.text._ZN4pqxx13string_traitsIiE4nullEv[_ZN4pqxx13string_traitsIiE4nullEv]+0x47): undefined reference to `pqxx::internal::throw_null_conversion(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cchvWstf.o: In function `pqxx::connect_direct::connect_direct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
test2.cpp:(.text._ZN4pqxx14connect_directC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4pqxx14connect_directC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x1f): undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cchvWstf.o: In function `pqxx::transaction<(pqxx::isolation_level)0, (pqxx::readwrite_policy)1>::transaction(pqxx::connection_base&)':
test2.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE]+0xba): undefined reference to `pqxx::dbtransaction::fullname(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test2.cpp:(.text._ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE[_ZN4pqxx11transactionILNS_15isolation_levelE0ELNS_16readwrite_policyE1EEC1ERNS_15connection_baseE]+0x190): undefined reference to `pqxx::basic_transaction::basic_transaction(pqxx::connection_base&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, pqxx::readwrite_policy)'我怎么编译它呢?我试着改变旗帜的顺序,但没有帮助。谢谢。
发布于 2017-05-11 22:38:48
我最近在尝试在Ubuntu 16.04上使用libpqxx-3.1编译代码时遇到了这个问题。同样的代码在Ubuntu 14.04中编译成功,但在Ubuntu 16.04中会出现链接器错误(与您的一样)。罪魁祸首似乎是gcc 4.8.4和gcc 5.4.0之间的libstdc++更改。根据https://gcc.gnu.org/gcc-5/changes.html上的“运行时库”文档
该库提供了双ABI。默认情况下会启用新的ABI。旧的ABI仍然受支持,并且可以通过在包含任何C++标准库头文件之前将宏_GLIBCXX_USE_CXX11_ABI定义为0来使用。
要确认这是否是您面临的问题,请将以下宏添加到cpp文件的顶部并进行编译。
#定义_GLIBCXX_USE_CXX11_ABI 0
如果链接成功,那么您已经确认您的代码正在使用std::string的新实现进行编译(就像gcc5在默认情况下所做的那样),而您要链接到的库使用旧的实现(就像gcc4所做的那样)。引入宏之后,您的代码和libpqxx将使用相同的实现。
长期的解决方案是使用与您正在使用的相同版本的gcc构建的库。我不确定为什么Ubuntu16.04发布了一个年长的gcc构建的库,这肯定让我很头疼。您可以坚持使用上面的解决方案,编译并安装您自己的libpqxx版本,或者升级到您的Linux发行版的更高版本(希望发布更新版本的库)。
发布于 2017-05-05 02:38:19
首先,检查您的libpxx.so是否正确安装在/usr/lib中。然后尝试使用g++ -L/usr/lib/ add_employee.cpp -o add_employee -lpqxx -lpq。这应该会起作用,并生成一个名为add_employee ( part -o add_employee)的可执行文件。
发布于 2019-06-19 14:45:43
请在代码中添加名称命名空间pqxx
https://stackoverflow.com/questions/42096935
复制相似问题