我正在尝试测试与postgreSQL数据库的连接。我从这里安装了 libpqxx The official C++ client API for PostgreSQL。在使用默认设置执行./configure PG_CONFIG=/usr/pgsql-10/bin/pg_config
和make
之前,我运行make install
进行配置。我必须先安装postgresql10-libs
和postgresql10-devel
才能拥有 pg_config 文件,因为我的PC上没有真正的PostgreSQL服务器。
我正在尝试使用标记-lpqxx -lpq
进行编译。我已将此添加到 CMakeLists.txt :
set(GCC_COVERAGE_COMPILE_FLAGS "-lpqxx -lpq -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
但我一直收到这个错误:
Linking CXX executable myproject
/usr/bin/ld: cannot find -lpq
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/myproject.dir/build.make:1187: myproject] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myproject.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/myproject.dir/rule] Error 2
gmake: *** [Makefile:118: myproject] Error 2
更新12.06.18
我已安装postgresql-devel
并已更改
set(GCC_COVERAGE_COMPILE_FLAGS "-lpqxx -lpq -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
到
set(GCC_COVERAGE_COMPILE_FLAGS "-std=gnu++11")
set(PQXX_AND_PQ_FLAGS "-lpqxx -lpq -I/usr/local/include -L/usr/local/lib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PQXX_AND_PQ_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
错误/usr/bin/ld: cannot find -lpq
已消失。
但现在我收到了一些关于 pqxx
的其他错误CMakeFiles/myproject.dir/dev/hmmenc_client/db/db.cpp.o: In function `db::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
/home/tglozman/CLionProjects/myproject/dev/hmmenc_client/db/db.cpp:17: undefined reference to `pqxx::connection_base::is_open() const'
/home/tglozman/CLionProjects/myproject/dev/hmmenc_client/db/db.cpp:18: undefined reference to `pqxx::connection_base::dbname()'
/home/tglozman/CLionProjects/myproject/dev/hmmenc_client/db/db.cpp:23: undefined reference to `pqxx::connection_base::disconnect()'
CMakeFiles/myproject.dir/dev/hmmenc_client/db/db.cpp.o: In function `pqxx::connection_base::connection_base(pqxx::connectionpolicy&)':
/usr/local/include/pqxx/connection_base.hxx:695: undefined reference to `int pqxx::internal::check_library_version<6, 2>()'
/usr/local/include/pqxx/connection_base.hxx:698: undefined reference to `pqxx::connection_base::clearcaps()'
CMakeFiles/myproject.dir/dev/hmmenc_client/db/db.cpp.o: In function `pqxx::connect_direct::connect_direct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/pqxx/connection.hxx:83: undefined reference to `pqxx::connectionpolicy::connectionpolicy(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/include/pqxx/connection.hxx:83: undefined reference to `vtable for pqxx::connect_direct'
CMakeFiles/myproject.dir/dev/hmmenc_client/db/db.cpp.o: In function `pqxx::connect_direct::~connect_direct()':
/usr/local/include/pqxx/connection.hxx:78: undefined reference to `vtable for pqxx::connect_direct'
/usr/local/include/pqxx/connection.hxx:78: undefined reference to `pqxx::connectionpolicy::~connectionpolicy()'
CMakeFiles/myproject.dir/dev/hmmenc_client/db/db.cpp.o: In function `pqxx::basic_connection<pqxx::connect_direct>::basic_connection(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/pqxx/basic_connection.hxx:57: undefined reference to `pqxx::connection_base::init()'
CMakeFiles/myproject.dir/dev/hmmenc_client/db/db.cpp.o: In function `pqxx::basic_connection<pqxx::connect_direct>::~basic_connection()':
/usr/local/include/pqxx/basic_connection.hxx:66: undefined reference to `pqxx::connection_base::close()'
collect2: error: ld returned 1 exit status
我在使用#include <pqxx/pqxx>
答案 0 :(得分:0)
你可能需要告诉cmake&#39; pq&#39;和&#39; pqxx&#39;已安装。通过指定一个完整的路径&l -lfullpath-to-pq&#39;或使用link_directories