Main Tutorials

How to building PostgreSQL libpq Programs

Create and compile a program with PostgreSQL libpq is not so straightforward. I created a sample program like “testlibpq.c” in PostgreSQL documentation to test it.

When i compile it, i hit following error

libpq-fe.h: No such file or directory
PGconn’ undeclared (first use in this function)

mkyong@mkyong-desktop:~/Desktop/index$ gcc -o test.o test.c
test.c:8:22: error: libpq-fe.h: No such file or directory
test.c:11: error: expected ‘)’ before ‘*’ token
test.c: In function ‘main’:
test.c:21: error: ‘PGconn’ undeclared (first use in this function)
test.c:21: error: (Each undeclared identifier is reported only once
test.c:21: error: for each function it appears in.)
test.c:21: error: ‘conn’ undeclared (first use in this function)
test.c:22: error: ‘PGresult’ undeclared (first use in this function)
test.c:22: error: ‘res’ undeclared (first use in this function)
test.c:41: error: ‘CONNECTION_OK’ undeclared (first use in this function)
test.c:57: error: ‘PGRES_COMMAND_OK’ undeclared (first use in this function)
test.c:83: error: ‘PGRES_TUPLES_OK’ undeclared (first use in this function)

It do need to include -I/usr/include/postgresql/ in order to compile it, please check your database or Linux administrator, or just type “pg_config –includedir” to find out where is PostgreSQL include file located.

However I still hit error like follwing

undefined reference to `PQfinish’
undefined reference to `PQconnectdb’

mkyong@mkyong-desktop:~/Desktop/index$ gcc -I/usr/include/postgresql/  -o test.o test.c
/tmp/ccoOJzAT.o: In function `exit_nicely':
test.c:(.text+0xd): undefined reference to `PQfinish'
/tmp/ccoOJzAT.o: In function `main':
test.c:(.text+0x57): undefined reference to `PQconnectdb'
test.c:(.text+0x65): undefined reference to `PQstatus'
test.c:(.text+0x74): undefined reference to `PQerrorMessage'
test.c:(.text+0xac): undefined reference to `PQexec'
test.c:(.text+0xba): undefined reference to `PQresultStatus'
test.c:(.text+0xca): undefined reference to `PQerrorMessage'
test.c:(.text+0xef): undefined reference to `PQclear'
test.c:(.text+0x105): undefined reference to `PQclear'
test.c:(.text+0x118): undefined reference to `PQexec'

Library is missing. Include PostgreSQL library pathwith -L/usr/lib/postgresql/8.3/lib/, this all PostgreSQL configuration information can get it from “pg_config”.

But I still hit the same error again

undefined reference to `PQfinish’
undefined reference to `PQconnectdb”

mkyong@mkyong-desktop:~/Desktop/index$ gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -o test.o test.c
/tmp/ccgWnRJg.o: In function `exit_nicely':
test.c:(.text+0xd): undefined reference to `PQfinish'
/tmp/ccgWnRJg.o: In function `main':
test.c:(.text+0x57): undefined reference to `PQconnectdb'
test.c:(.text+0x65): undefined reference to `PQstatus'
test.c:(.text+0x74): undefined reference to `PQerrorMessage'
test.c:(.text+0xac): undefined reference to `PQexec'
test.c:(.text+0xba): undefined reference to `PQresultStatus'
test.c:(.text+0xca): undefined reference to `PQerrorMessage'
test.c:(.text+0xef): undefined reference to `PQclear'
test.c:(.text+0x105): undefined reference to `PQclear'
test.c:(.text+0x118): undefined reference to `PQexec'
test.c:(.text+0x126): undefined reference to `PQresultStatus'
test.c:(.text+0x136): undefined reference to `PQerrorMessage'
test.c:(.text+0x15b): undefined reference to `PQclear'
test.c:(.text+0x171): undefined reference to `PQclear'

It do need to specified exactly library with -lpq to include libpg library, correct command is like following

gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -lpq -o test.o test.c

Done, compile without error.

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
the_illinois_enema_bandit
12 years ago

On my Ubuntu -lpq MUST be after -o test.o test.c

logrusorgru
9 years ago

Thanks a lot!!!

tbag
13 years ago

I heard the same problem and i just did:

apt-get install libpq-dev

all my problems were gone.

wally
3 years ago
Reply to  tbag

That worked for me!

Ricardo Wagemaker
3 years ago

I have to reverse the order of things:::

Failed:
gcc -I/usr/include/postgresql -L/usr/lib/postgresql/12/lib/ -lpq -std=c99 -o server_version server_version.c

Worked:
gcc -o server_version server_version.c -I/usr/include/postgresql -L/usr/lib/postgresql/12/lib/ -lpq -std=c99

Neena
11 years ago

Thanks!

Lacy
12 years ago

Thanks so much, you saved my day.

César Mata Moya
14 years ago

Thanks, Gracias

Alex
15 years ago

Hello, instead of setting -I, -L and -l manually you should better use pg_config