免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3753 | 回复: 0
打印 上一主题 下一主题

Postgresql-libpq-编译和链接 libpq 程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-17 10:06 |只看该作者 |倒序浏览
http://www.wohedb.com/db_html_doc/libpq_api/libpq_11_build.htm
http://www.wohedb.com  中文数据库管理系统

1.11 编译和链接 libpq 程序
   使用下面的步骤编译和链接libpq 程序:

(1)    包含libpq-fe.h 头文件

#include <libpq-fe.h>
如果没有包含libpq-fe.h 头文件,编译libpq 程序时会得到类似下面的错误:

foo.c: In function `main':
foo.c:34: `PGconn' undeclared (first use in this function)
foo.c:35: `PGresult' undeclared (first use in this function)
foo.c:54: `CONNECTION_BAD' undeclared (first use in this function)
foo.c:68: `PGRES_COMMAND_OK' undeclared (first use in this function)
foo.c:95: `PGRES_TUPLES_OK' undeclared (first use in this function)
(2)    使用-Idirectory 选项告诉编译器存放libpq-fe.h 头文件的目录,例如:

cc -c -I/usr/local/pgsql/include testprog.c
如果使用makefile来编译程序,把下面的选项加到CPPFLAGS变量中:

CPPFLAGS += -I/usr/local/pgsql/include
可以使用数据库提供的工具pg_config来确定libpq-fe.h 头文件所在的目录,在操作系统中执行下面的命令即可:

$ pg_config --includedir
/usr/local/include
如果编译器找不到libpq-fe.h 头文件,会报类似下面的错误:

testlibpq.c:8:22: libpq-fe.h: No such file or directory
(3)    链接libpq 程序时,必须使用选项-lpq和-Ldirectory。选项-Ldirectory指出存放libpq共享库文件的目录。注意,为了提高兼容性,-Ldirectory应该出现在-lpq的前面。例如:

cc -o testprog testprog1.o testprog2.o -L/usr/local/pgsql/lib -lpq
可以使用数据库提供的工具pg_config来确定存放libpq共享库文件的目录:

$ pg_config --libdir
/usr/local/pgsql/lib
如果没使用-lpq选项,会遇到类似下面的错误:

testlibpq.o: In function `main':
testlibpq.o(.text+0x60): undefined reference to `PQsetdbLogin'
testlibpq.o(.text+0x71): undefined reference to `PQstatus'
testlibpq.o(.text+0xa4): undefined reference to `PQerrorMessage'
如果-Ldirectory选项指定的存放libpq共享库文件的目录不正确,会遇到下面的错误:

/usr/bin/ld: cannot find -lpq
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP