Search code examples
c++postgresqlg++libpq

Undefined reference to PQfinish even though libraries etc. are included


I'm trying to compile a C++ application using libpq. On my Debian it compiles fine with the following command:

g++ -I /usr/include/postgresql/ -L /usr/lib/postgresql/9.3/lib/ -lpq -o test test.cpp

On my Ubuntu I get the following error:

undefined reference to 'PQfinish'

I've included link to postgresql headers and libraries and used the -lpq. What am I missing?


Solution

  • Move -lpq to the end of the command line.

    Linking is done in the order specified, and objects are only taken from libraries if they are needed to resolve symbols at that point. So each library has to come after any other object that needs it.