C++ 无法使用C+;连接到PostgreSQL+;
我正试图在我的mac上运行PostgreSQL。PostgreSQL本身很好,我可以创建数据库和表,但当我尝试用C++连接PostgreSQL时,使用类似的:C++ 无法使用C+;连接到PostgreSQL+;,c++,macos,postgresql,C++,Macos,Postgresql,我正试图在我的mac上运行PostgreSQL。PostgreSQL本身很好,我可以创建数据库和表,但当我尝试用C++连接PostgreSQL时,使用类似的: #include <stdio.h> #include </Library/PostgreSQL/8.4/include/libpq-fe.h> #include <string> int main() { PGconn *conn; PGresult *res
#include <stdio.h>
#include </Library/PostgreSQL/8.4/include/libpq-fe.h>
#include <string>
int main() {
PGconn *conn;
PGresult *res;
int rec_count;
conn = PQconnectdb("dbname=ljdata host=localhost user=dataman);
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn, "update people set phonenumber=\'5055559999\' where id=3");
我得到了错误
ld:找不到-lpq的库
如果我编译时没有lpq,我会
Undefined symbols:
"_PQclear", referenced from:
_main in ccpjNCAU.o
_main in ccpjNCAU.o"
我已经包括了libpq fe.h,它不应该工作吗?有人知道出了什么问题吗?g++找不到
pq
库。您必须使用大写字母-L
指定在何处查找它:
g++ -L/path/to/pq/lib -lpq db.cpp -o db
其中pq
是/path/to/pq/lib/libpq.a(或任何扩展名)
以下是您可能想要做的:
#include "libpq-fe.h"
g++ -I/Library/PostgreSQL/8.4/include db.cpp
g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -o db.o
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq
-g
g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -g -o db.o
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq -o db
libpq fe.h
是一个用户库,而不是系统库,因此您应该使用“…”
而不是
,如下所示:
#include "/Library/PostgreSQL/8.4/include/libpq-fe.h"
看一看。并确保编译器确实可以找到
libpq fe.h
。也有同样的问题,您需要将库的路径添加到/etc/ld.so.conf
,这样做,您就会看到。
祝你好运我想我的编译器可以找到libpq fe.h。一旦我将#include“/Library/PostgreSQL/8.4/include/libpq fe.h”更改为#include“Library/PostgreSQL/8.4/include/libpq fe.h”,我会得到错误:“PGconn”未在此范围内声明错误:“conn”未在此范围内声明。PGconn已经定义好了,这不意味着编译器可以找到libpq fe.h吗?所以如果我的文件在家里,我应该只做g++-L/Library/PostgreSQL/8.4/include/libpq fe.h?那也不行。-L/path/to/lib/dir not/path/to/header/file。libpq位于何处?libpq fe.h和libpq events.h位于Library/PostgreSQL/8.4/include/中,但Library/PostgreSQL/8.4/include/libpq中还有一个“libpq fs.h”,它们是头文件-它们以
.h
结尾。实际的库文件在哪里?请尝试查找library/PostgreSQL/8.4/lib
是否在那里?如果是这样,请尝试使用-L~/Library/PostgreSQL/8.4/lib
#include "/Library/PostgreSQL/8.4/include/libpq-fe.h"