2013-04-09 74 views
0

當我在cpp代碼中的一些客戶端postgresql我一直有這個簡單的代碼有問題:連接到PostgreSQL在Windows

#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上的此庫?

回答

3

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標誌,看看你提到的其他錯誤消失。

0

您的libpq-fe.h文件包含: extern PGconn * PQsetdbLogin ??

還檢查你的庫。可能它是一個缺少libq連接器。

嘗試在構建項目時包含include/postgresql -L/lib -lpq。