我刚刚安装了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,OS:Ubuntu
答案 0 :(得分:2)
&#39;未定义的引用&#39;错误意味着您没有将已编译的代码链接到库,因为程序需要使用库才能正常运行。您应该能够通过将代码链接到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数据库运行时具有正确的访问权限和数据库表,但这不再是编程或编译问题,而是管理问题。
希望有所帮助。