当我在 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/