使用PostgreSQL libpq创建和编译程序并不是那么简单。 我在PostgreSQL文档中创建了一个示例程序(如“ testlibpq.c”)对其进行测试。
当我编译它时,我遇到以下错误
libpq-fe.h:没有这样的文件或目录
PGconn'未声明(此功能的首次使用)
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)
它确实需要包含-I / usr / include / postgresql /才能进行编译,请检查您的数据库或Linux管理员,或者只是键入“ pg_config –includedir”以查找PostgreSQL包含文件在哪里。
但是我仍然遇到如下错误
未定义对“ PQfinish”的引用
未定义对“ 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'
图书馆不见了。 在-L / usr / lib / postgresql / 8.3 / lib /中包含PostgreSQL库路径,所有这些PostgreSQL配置信息都可以从“ pg_config”中获取。
但我仍然再次遇到相同的错误
未定义对“ PQfinish”的引用
未定义对“ 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'
它确实需要使用-lpq精确指定库以包含libpg库,正确的命令如下所示
gcc -I/usr/include/postgresql/ -L/usr/lib/postgresql/8.3/lib/ -lpq -o test.o test.c
完成,编译没有错误。
标记: PostgreSQL
翻译自: https://mkyong.com/database/how-to-building-postgresql-libpq-programs/