我安装pqxx错了吗?

Did I install pqxx wrong?

我刚刚安装了 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:代码块,OS:Ubuntu

'undefined reference' 错误意味着您没有 link 编译代码到库,因为程序需要使用库才能 运行 正确。您应该能够通过 link 将代码编译为 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 命令,然后 link 将其编译为您的编译代码。

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

希望对您有所帮助。