Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用C+;连接到PostgreSQL+;_C++_Macos_Postgresql - Fatal编程技术网

C++ 无法使用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

我正试图在我的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;
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"
    
  • 将include路径添加到命令行

    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"