postgresql problem linking libpq with c program using gcc 12.04

Asked by Philippe Van Schendel

This code does not link with libpq: i have undefined reference to 'PQconnectdb'

i do: gcc -I /usr/include/postgresql -L/usr/lib -lpq -o pvs pvs.cpp

libpq.a is in /usr/lib directory size 301928, i have a brand new 12.04 (64 bits) system with postgres 9.1, i have installed package libpq-dev

when i do 'nm libpq.a' i can see PQconnectdb symbols, the linker do not see the PDconnectdb symbol in libpq.a

i dont understand the problem.. no clue. is it 32/64 bits problem, c/c++ name mangling ?

Source code here:

#include "libpq-fe.h"
int main(int argc, char *argv[])
{
 PGconn *pgConn = PQconnectdb("dbname=xxx user=yyy password=zzz");
 if (pgConn != NULL)
 {
  printf("Connected to database.\n");
 }
 else
 {
  printf("Cannot connect to database.\n");
 }
}

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu postgresql-8.3 Edit question
Assignee:
No assignee Edit question
Solved by:
Ubfan
Solved:
Last query:
Last reply:
Revision history for this message
Best Ubfan (ubfan1) said :
#1

Move the -lpq to the end of the line, then it works.

Revision history for this message
Philippe Van Schendel (philippe-vanschendel) said :
#2

Thanks Ubfan, that solved my question.