c++ - 在 Windows 上连接到 postgresql

标签 c++ postgresql

当我在 cpp 中为 postgresql 编写一些客户端代码时,我总是遇到这个简单代码的问题:

#include <iostream>
#include <libpq-fe.h>
#include <stdio.h>
using namespace std;

char pghost[] = "localhost";
char pgport[] = "5432";
char pgbase[] = "baza";
char pguser[] = "postgres";
char pgpass[] = "server";
char conninf[] = "host=localhost port=5432 database=postgres user=postgress password=server";

int main(){    
  PGconn *conn = PQsetdbLogin(pghost,pgport,NULL,NULL,pgbase,pguser,pgpass);

  if (PQstatus(conn) == CONNECTION_OK)
  {
    //if connected
    cout<<"Ok.\n";
  }
  else
  {  
    cout<<"ERROR: %s\n";
    PQfinish(conn);
    }
    return 0;
  }
}        

我有一个错误输出

C:\Users\Tom\Desktop\connect\connect.cpp|16|undefined reference to `PQsetdbLogin'| 

以及库中其他函数的许多错误。

我做错了什么?如何在 Windows 上正确连接到此库?

最佳答案

来自docs :

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'

This means you forgot -lpq.

尝试传递 -lpq 标志并查看您提到的其他错误是否消失。

关于c++ - 在 Windows 上连接到 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15900322/

相关文章:

c++ - 命名包括 guard

c++ - 我可以向编译器查询 C++0x "alignas"支持吗?

c++ - 友元函数返回 (void*) : cannot get implementation in . cpp 文件工作

ruby-on-rails - 必须是 super 用户才能创建此扩展

database - 依赖追踪功能

c++ - 使用 mbsnrtowcs 进行多字节到 Widechar 的转换

c++ - 有没有办法消除这种歧义?

SQL:根据另一列将一列求和为多列

ruby-on-rails - rails 表单不适用于 postgresDB

Java PreparedStatement 无法识别子查询中的占位符