0

Using MSYS, I compiled libpq (from compiling postgres). I then compiled libpqxx. Now, I want to create a client that will use libpqxx. libpq seemed to work fine. And, I can compile code with libpqxx. However, linking the libpq client application fails.

Here's my code:


#include <pqxx/pqxx>
#include <iostream>
using namespace std;
using namespace pqxx;

int main() {
    connection Conn("dbname=test");
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

I added a bunch of libs to the link in a vain hope it would suddenly work. Here's what I have so far:

g++ -IC:\msys\1.0\local\pgsql\include -IC:\msys\1.0\local\include -O0 -g3 -Wall -c -fmessage-length=0 -osrc\Controller.o ..\src\Controller.cpp
g++ -LC:\MinGW\lib -LC:\msys\1.0\local\pgsql\lib -LC:\msys\1.0\local\lib -oController.exe src\Controller.o -lws2_32 -lole32 -lpqxx -lpq -loleaut32 -luuid
C:\msys\1.0\local\lib/libpqxx.a(connection_base.o): In function `ZN45_GLOBAL__N__ZN4pqxx16encrypt_passwordERKSsS1_7wait_fdEibP7timeval':
C:/msys/1.0/home/rsolomon/libpqxx/libpqxx-3.0.2/src/connection_base.cxx:1434: undefined reference to `select@20'
C:\msys\1.0\local\lib/libpqxx.a(connection_base.o): In function `ZN4pqxx15connection_base12check_resultERKNS_6resultE':
C:/msys/1.0/home/rsolomon/libpqxx/libpqxx-3.0.2/src/connection_base.cxx:420: undefined reference to `select@20'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 1770  ms.  

I'm thinking the -lws2_32 should've gave me the "select@20". Why is the linker so uppity?

1 Answer 1

3

The Unix linker traditionally processes libraries from left to right. So it first considers ws2_32, finds that it has not much use, then goes on to pqxx, and sees that select is undefined and doesn't get defined by any of the later libraries. IOW, try moving ws2_32 to the end of the command line.

3
  • Wow! It worked. So, the order of the -l should go from specific libraries that I use first to the most transitive libraries last? I guess I found that one out the hard way.
    – User1
    Sep 2, 2009 at 13:09
  • Would I ever need a -l for the same lib twice?
    – User1
    Sep 2, 2009 at 13:11
  • 1
    Unix traditionally has tsort(1) to compute the topological order, even though it's not widely used... In any case, yes, you might have to add libraries twice if there is a cyclic dependency between libraries - which you shouldn't have if your libraries are properly stacked. Sep 2, 2009 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.