Search code examples
c++ubuntucmakewindows-subsystem-for-linuxlibpqxx

Including libpqxx results in failed build on WSL using CMake


I'm currently in the process of upgrading a C++ application make use of a PostgreSQL database. Since we are typically running on *nix based systems I'm currently working within the Windows Subsystem for Linux (WSL) and a Ubuntu distribution in particular. PostgreSQL 10 was install via apt-get and everything was upgraded before following the directions to build libpqxx on Unix-like systems.

As a very basic test I have a simple class that just includes the reference:

#include <pqxx/pqxx> 

However, building results in a (long) error log starting with the following:

[ 15%] Building CXX object src/CMakeFiles/Core.dir/Reporters/DbReporter.cpp.o
In file included from /usr/local/include/pqxx/array.hxx:18:0,
                 from /usr/local/include/pqxx/array:4,
                 from /usr/local/include/pqxx/pqxx:2,
                 from /mnt/c/Users/User/git/Simulation/src/Reporters/DbReporter.cpp:3:
/usr/local/include/pqxx/internal/encodings.hxx:26:42: error: ‘pqxx::internal::encoding_group pqxx::internal::enc_group’ redeclared as different kind of symbol
 encoding_group enc_group(std::string_view);

All of the errors that follow are of a similar nature with files under /pqxx/*. generating the errors. Typically for invalid declarations, functions not being part of the std namespace (which is not redefined in the application), or what appear to be syntax errors.

I suspect there is a mis-match between how the libpqxx library was built. How can I isolate and correct the problem?


Solution

  • While the logs from the build were a bit hard to parse; however, the line redeclared as different kind of symbol is a clue along with errors in the output such as the following:

    /usr/local/include/pqxx/stream_from.hxx: In constructor ‘pqxx::stream_from::stream_from(pqxx::transaction_base&, const string&, Iter, Iter ’:
    /usr/local/include/pqxx/stream_from.hxx:134:19: error: missing template arguments before ‘(’ token
         separated_list(",", columns_begin, columns_end)
    

    These strongly imply that there is a syntax error in the code, despite the library building fine with the tool chain outlined by the developer. One significant point is that libpqxx is dependent upon C++17. The error ended up being in the CMakeLists.txt file:

    set(CMAKE_CXX_STANDARD 14)
    

    Indicating that despite the library being dependent upon C++17, the application was building with C++14. Update line to 17 corrected the problem and allowed the code to build. At which point CMakeLists.txt must also be updated to link correctly:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpqxx -lpq")
    set(PQXX /usr/local/include/pqxx)
    
    find_library(PQXX_LIB pqxx)
    find_library(PQ_LIB pq)
    
    target_link_libraries(PROGRAM ${PQXX_LIB} ${PQ_LIB})