我刚刚安装了libpqxx(postgresql for c++)。我正在遵循本教程: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:CodeBlocks,操作系统:Ubuntu
未定义的引用"错误意味着您没有将编译的代码链接到库,因为程序需要使用库才能正常运行。您应该能够通过将代码链接到 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 数据库以正确的访问权限和数据库表运行,但这不再是编程或编译问题,而是管理问题。
希望有帮助。