-2

我刚刚安装了 libpqxx(用于 c++ 的 postgresql)。我正在关注本教程:http ://www.tutorialspoint.com/postgresql/postgresql_c_cpp.htm 一切都很好,少一件事。当我尝试编译示例代码时,我看到了所有这些错误:

/home/JakisUzytkownik/Hobby/C++/DzialaNaUbuntu.o||In function `main':|
DzialaNaUbuntu.cpp|| undefined reference to `pqxx::connection_base::is_open() const'|
DzialaNaUbuntu.cpp|| undefined reference to `pqxx::connection_base::dbname()'|
DzialaNaUbuntu.cpp|| undefined reference to `pqxx::connection_base::disconnect()'|
/home/marcwel/Hobby/C++/DzialaNaUbuntu.o||In function `pqxx::connect_direct::connect_direct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':|
DzialaNaUbuntu.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&)'|
DzialaNaUbuntu.cpp:(.text._ZN4pqxx14connect_directC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN4pqxx14connect_directC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x24)||undefined reference to `vtable for pqxx::connect_direct'|
/home/marcwel/Hobby/C++/DzialaNaUbuntu.o||In function `pqxx::connect_direct::~connect_direct()':|
DzialaNaUbuntu.cpp:(.text._ZN4pqxx14connect_directD2Ev[_ZN4pqxx14connect_directD5Ev]+0xd)||undefined reference to `vtable for pqxx::connect_direct'|
DzialaNaUbuntu.cpp:(.text._ZN4pqxx14connect_directD2Ev[_ZN4pqxx14connect_directD5Ev]+0x20)||undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'|
/home/marcwel/Hobby/C++/DzialaNaUbuntu.o||In function `pqxx::basic_connection<pqxx::connect_direct>::basic_connection(char const*)':|
DzialaNaUbuntu.cpp:(.text._ZN4pqxx16basic_connectionINS_14connect_directEEC2EPKc[_ZN4pqxx16basic_connectionINS_14connect_directEEC5EPKc]+0x38)||undefined reference to `pqxx::connection_base::connection_base(pqxx::connectionpolicy&)'|
DzialaNaUbuntu.cpp:(.text._ZN4pqxx16basic_connectionINS_14connect_directEEC2EPKc[_ZN4pqxx16basic_connectionINS_14connect_directEEC5EPKc]+0xc3)||undefined reference to `pqxx::connection_base::init()'|
/home/marcwel/Hobby/C++/DzialaNaUbuntu.o||In function `pqxx::basic_connection<pqxx::connect_direct>::~basic_connection()':|
DzialaNaUbuntu.cpp:(.text._ZN4pqxx16basic_connectionINS_14connect_directEED2Ev[_ZN4pqxx16basic_connectionINS_14connect_directEED5Ev]+0x17)||undefined reference to `pqxx::connection_base::close()'| 

IDE:代码块,操作系统:Ubuntu

4

1 回答 1

2

“未定义引用”错误意味着您没有将编译后的代码链接到库,因为程序需要使用库才能正常运行。您应该能够通过将代码链接到 libpqxx 和 libpq 来编译教程中的代码;

g++ your_code.cpp -lpqxx -lpq -o your-exec

如果未安装在默认系统库中,您可能需要通过将 -L 添加到编译命令来手动提供库的路径,如下所示;

g++ your_code.cpp -L../path-to-libpqxx -lpqxx -lpq -o your-exec

但是,如果确实该库安装在正确的位置,那么您可能必须通过发出 sudo ldconfig命令确保该库已由系统上传,然后再将其链接到您的编译代码。

要成功运行代码,您还需要确保您的 postgresql 数据库以正确的访问权限和数据库表运行,但这不再是编程或编译问题,而是管理问题。

希望有帮助。

于 2016-01-24T09:48:29.027 回答