views:

123

answers:

1

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?

+2  A: 

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.

Martin v. Löwis
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
Would I ever need a -l for the same lib twice?
User1
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.
Martin v. Löwis