Thread: Trouble linking to libpq

Trouble linking to libpq

From
"Nathan S."
Date:
Greetings,

I am writing a Multi-User Dungeon (MUD) codebase that will be pulling
its object information from a postgres database.  I am interfacing with
the database via libpq.

The problem I am having is that I cannot link to libpq, and I cannot
figure out why.

The following snippet compiles and links fine by itself:

--- begin ---
#include <stdio.h>
#include <libpq-fe.h>

int main(int argc, char **argv)
{
  PGconn *conn;

  conn = PQconnectdb("");
  return 0;
}
--- end ---
(compiler commandline for above: gcc test.c -o test -lpq)
However, if I take the same snippet and try to link it along with other
files in my project, here's the error I get:

--- begin ---
gcc -o test -lpq main.o string.o memory.o network.o event.o command.o
player.o tables.o lookup.o db.o
db.o(.text+0x10f):db.c: undefined reference to `_PQconnectdb'
collect2: ld returned 1 exit status
make: *** [test] Error 1
--- end ---

And, for good measure, here's my Makefile:
--- begin ----
O_FILES=main.o string.o memory.o network.o event.o command.o player.o
tables.o lookup.o db.o
L_FLAGS=-lpq
C_FLAGS=

EXE=test

$(EXE): $(O_FILES)
     gcc -o test $(L_FLAGS) $(O_FILES)
.c.o:
     gcc $(C_FLAGS) -c $(C_FLAGS) $<
clean:
     rm -f $(EXE) $(O_FILES)
--- end ---

Cygwin is the newest 1.3.x release and the postgres package is 7.3.4-1.

Any suggestions?

Nathan

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

Re: Trouble linking to libpq

From
Claudio Natoli
Date:
Replace your makefile line:

>      gcc -o test $(L_FLAGS) $(O_FILES)

with

     gcc -o test $(O_FILES) $(L_FLAGS)

and I think you'll have better luck.

Cheers,
Claudio

---
WE HAVE MOVED - PLEASE NOTE OUR NEW CONTACT DETAILS:
THE BASEMENT, 33 EWELL STREET, BALMAIN NSW 2041
TEL: +61 2 9555 1544 FAX: +61 2 9555 6911
Certain disclaimers and policies apply to all email sent from Memetrics.
For the full text of these disclaimers and policies see
http://www.memetrics.com/emailpolicy.html