linux下c程序使用postgresql 8.4连接数据库时编译出现的问题。
1.
问题:In function `main':
`PGconn' undeclared (first use in this function)
`PGresult' undeclared (first use in this function)
`CONNECTION_BAD' undeclared (first use in this function)
`PGRES_COMMAND_OK' undeclared (first use in this function)
`PGRES_TUPLES_OK' undeclared (first use in this function)
出现此问题,为缺少#include <libpq-fe.h>
2.
问题:libpq-fe.h: No such file or directory
解决:系统未查找到libpq-fe.h,需要指定其目录
方式1:gcc -c -I/usr/local/pgsql/include test.c
方式2:Makefile文件中,CPPFLAGS += -I/usr/local/pgsql/include
3.编译.c至.o通过,但链接时出现如下问题
In function `main':
undefined reference to `PQsetdbLogin'
undefined reference to `PQstatus'
undefined reference to `PQerrorMessage'
解决:此为缺少链接所需的库文件导致,需手动指定其目录
undefined reference to `PQsetdbLogin'
undefined reference to `PQstatus'
undefined reference to `PQerrorMessage'
解决:此为缺少链接所需的库文件导致,需手动指定其目录
gcc -o test test1.o test2.o -L/usr/local/pgsql/lib -lpq
编译成功。
参考:http://www.postgresql.org/docs/8.4/interactive/libpq-build.html
总结:更多详细至官方搜索解决方案,google时多使用site:来限定搜索范围,更快速找到问题解决方法。