Debian中库libpq的问题

时间:2011-09-19 10:14:20

标签: c postgresql gcc debian libpq

我在Debian中安装postgresql 8.4,将程序testlibpq.c从http://www.postgresql.org/docs/9.0/interactive/libpq-example.html放到有文件libpq-fe.h的目录下,但编译完成后gcc写信给我

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.我必须要做些什么来纠正工作?

1 个答案:

答案 0 :(得分:7)

看起来你没有链接PostgreSQL库。您应该使用以下内容编译testlibpq.c

gcc -o testlibpq testlibpq.c -lpq

-lpq告诉链接器链接PostgreSQL库,PQconnectdb和朋友来自哪里。

你可能需要告诉编译器在哪里找到库和头文件,如果有的话,那么这样的东西应该排除:

gcc -o testlibpq -I$(pg_config --includedir) -L$(pg_config --libdir) -o testlibpq $(pg_config --libs)