gpt4 book ai didi

c++ - 编译 libpqxx 代码示例时出错?

转载 作者:行者123 更新时间:2023-11-29 11:49:34 25 4
gpt4 key购买 nike

我正在尝试编译 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)'

如何编译它?我试图改变标志的顺序,但它没有帮助。谢谢。

最佳答案

我最近在 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 的“运行时库”文档

A Dual ABI is provided by the library. A new ABI is enabled by default. The old ABI is still supported and can be used by defining the macro _GLIBCXX_USE_CXX11_ABI to 0 before including any C++ standard library headers.

要确认这是否是您面临的问题,请将以下宏添加到您的 cpp 文件的顶部并编译。

#define _GLIBCXX_USE_CXX11_ABI 0

如果它链接成功,那么你已经确认你的代码正在使用 std::string 的新实现进行编译(默认情况下 gcc5 会这样做),而你链接到的库使用旧的实现(如gcc4 会做)。引入宏后,您的代码和 libpqxx 都使用相同的实现。

长期的解决方案是使用与您正在使用的相同版本的 gcc 构建的库。我不确定为什么 Ubuntu 16.04 会附带由旧版 gcc 构建的库,这肯定让我很头疼。您可以坚持使用上述解决方案,编译并安装您自己构建的 libpqxx,或者升级到更高版本的 Linux 发行版(希望发布更新版本的库)。

关于c++ - 编译 libpqxx 代码示例时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42096935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com