Search code examples
c++postgresqlmacoslibpqxx

Undefined symbols (link error) when using libpqxx


I can't seem to link libpqxx.

The program:

#include <pqxx/pqxx>
int main()
    pqxx::connection connection(""); 
    return 0;
}

The command to compile:

g++ -std=c++23 -arch arm64 x.cc -lpqxx -lpq -I/usr/local/include -L/usr/local/libc-L/opt/homebrew/opt/libpq/lib -I/opt/homebrew/opt/libpq/include

I'm on macOS Sequoia 15.3.1

I've tried this many ways:

  1. brew install libpqxx and use those includes and libraries in the compilation command
  2. built libpqxx myself (in usr/local/, which is what you can see in the command)
  3. I've also tried with cmake: a) fetch_content, b) with pkg-config, c) with the homebrew installation

It just does not build. This is usually the error I get:

Undefined symbols for architecture arm64:
  "pqxx::argument_error::argument_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)", referenced from:
      pqxx::internal::(anonymous namespace)::throw_for_encoding_error(char const*, char const*, unsigned long, unsigned long) in ccBebBm9.o
  "pqxx::internal::demangle_type_name[abi:cxx11](char const*)", referenced from:
      __static_initialization_and_destruction_0() in ccBebBm9.o
      __static_initialization_and_destruction_0() in ccBebBm9.o
      __static_initialization_and_destruction_0() in ccBebBm9.o
      __static_initialization_and_destruction_0() in ccBebBm9.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status

Solution

  • I've also tried with cmake: a) fetch_content

    Just test tried it out with fetch content and its working fine for me

    Given that you have pkg-config and libpq installed and findable, i.e. with brew.

    Then the following should work with the given structure

    ~/cpp-sandbox
    ├── CMakeLists.txt
    └── src
        └── main.cpp
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.22)
    project(cpp_sandbox)
    set(CMAKE_CXX_STANDARD 23)
    
    include(FetchContent)
    
    FetchContent_Declare(
            libpqxx
            GIT_REPOSITORY https://github.com/jtv/libpqxx.git
            GIT_TAG        7.10.0
    )
    
    FetchContent_MakeAvailable(libpqxx)
    
    add_executable(${PROJECT_NAME} src/main.cpp)
    target_link_libraries(${PROJECT_NAME} PRIVATE pqxx)
    

    src/main.cpp

    #include <pqxx/pqxx>
    
    int main() {
        pqxx::connection connection(""); 
        return 0;
    }
    

    building with

    cmake -DCMAKE_PREFIX_PATH="/opt/homebrew/opt/libpq"
    cmake -build .