2

I'm installing postgresql 8.4 in Debian, put program testlibpq.c from http://www.postgresql.org/docs/9.0/interactive/libpq-example.html to directory which have file libpq-fe.h, but after compilation gcc write me

testlibpq.c:(.text+0x4a): undefined reference to `PQconnectdb'
testlibpq.c:(.text+0x5a): undefined reference to `PQstatus'
testlibpq.c:(.text+0x6f): undefined reference to `PQerrorMessage'
testlibpq.c:(.text+0xa9): undefined reference to `PQexec'
testlibpq.c:(.text+0xb9): undefined reference to `PQresultStatus'
testlibpq.c:(.text+0xcf): undefined reference to `PQerrorMessage'
testlibpq.c:(.text+0xf5): undefined reference to `PQclear'
testlibpq.c:(.text+0x10d): undefined reference to `PQclear'
testlibpq.c:(.text+0x121): undefined reference to `PQexec' 

... e.t.c. What I must suppose to do to correct work?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
user710052
  • 41
  • 2
  • 3

1 Answers1

7

Looks like you're not linking the PostgreSQL library. You should be compiling testlibpq.c with something like this:

gcc -o testlibpq testlibpq.c -lpq

The -lpq tells the linker to link against the PostgreSQL library and that's where PQconnectdb and friends come from.

You may need to tell the compiler where to find the libraries and headers as well, if so then something like this should sort that out:

gcc -o testlibpq -I$(pg_config --includedir) -L$(pg_config --libdir) -o testlibpq $(pg_config --libs)
mu is too short
  • 426,620
  • 70
  • 833
  • 800