2

我在 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' 

...等我必须做些什么来纠正工作?

4

1 回答 1

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)
于 2011-09-19T10:24:16.410 回答