尝试使用c连接到postgresql时,为什么会出现编译错误?

时间:2011-05-27 10:37:13

标签: postgresql gcc libpq

我试图创建一个简单的c程序,它应该连接到我的postgres数据库,但是我无法了解以下编译错误... 我正在使用Ubuntu(但也尝试在Fedora上编译 - 具有相同的错误......)

uname --all
Linux alp2nwmon001 2.6.35-25-server #44-Ubuntu SMP Fri Jan 21 19:09:14 UTC 2011 x86_64 GNU/Linux

我已经安装了postgres包含libpq-dev libpq5 libs,我不知道为什么这个简单的代码不起作用...

#include "/usr/include/postgresql/libpq-fe.h"

int main () {
        PGConn *test;
        return 0;
}

我还尝试了很多变体,例如:#include <libpq-fe.h>,并在调用gcc时指定.h文件的路径。 但总是一样的错误...

gcc -c -I/usr/include/postgresql/ -L/usr/lib/ -lpq pqtest.c -o postgres
 pqtest.c: In function main:
 pqtest.c:4: error: PGConn undeclared (first use in this function)
 pqtest.c:4: error: (Each undeclared identifier is reported only once
 pqtest.c:4: error: for each function it appears in.)
 pqtest.c:4: error: test undeclared (first use in this function)

此外,我发现了许多声音,但结果仍然相同:

我还在postgres网页上检查了API描述,但仍然没有运气。

在我看来,所有文件都存在于应该存在的位置......

ls -lah /usr/include/postgresql/libpq-fe.h 
-rw-r--r-- 1 root root 20K 2011-04-20 16:36 /usr/include/postgresql/libpq-fe.h

图书馆......

ls /usr/lib/ | grep pq
libpq.a
libpq.so
libpq.so.5
libpq.so.5.2
libpqxx-2.6.9.so
libpqxx.la
libpqxx.so

我希望有人可以帮助我...... 谢谢你, roegi

2 个答案:

答案 0 :(得分:2)

使用PGconn代替PGConn

答案 1 :(得分:-1)

怎么样:

#include <pgconn.h>