0

I'm trying to test a connection to my postgreSQL database. I've installed libpqxx from here The official C++ client API for PostgreSQL. I ran ./configure PG_CONFIG=/usr/pgsql-10/bin/pg_config for configuring before executing make and make install with the default settings. I had to install postgresql10-libs and postgresql10-devel to be able to have the pg_config file in the first place, because my real PostgreSQL server is not on my PC.

I'm trying to compile with the flags -lpqxx -lpq. I've added this to my CMakeLists.txt:

set(GCC_COVERAGE_COMPILE_FLAGS "-lpqxx -lpq -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

But I keep getting this error:

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

UPDATE 12.06.18

I have installed postgresql-devel and changed

set(GCC_COVERAGE_COMPILE_FLAGS "-lpqxx -lpq -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

to

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}")

and the error /usr/bin/ld: cannot find -lpq is gone.

but now I'm getting some other errors regarding 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

I don't have any problems in the code with #include <pqxx/pqxx>

TalG
  • 677
  • 1
  • 9
  • 26

1 Answers1

0

You may need to tell cmake where the 'pq' and 'pqxx' are installed. Either by specifying a full path '-lfullpath-to-pq' or by using link_directories

André
  • 18,348
  • 6
  • 60
  • 74