Tomasz Tomsia
Tomasz Tomsia

Reputation: 1

Connect to postgresql on windows

When I code some client for postgresql in cpp I always have a problem with this simple code:

#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;
  }
}        

I have an error output

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

and a lot of errors to other functions from the library.

What I'm doing wrong? How do I connect to this library on Windows correctly?

Upvotes: 0

Views: 2270

Answers (2)

imamalis
imamalis

Reputation: 65

Your libpq-fe.h file contains the : extern PGconn *PQsetdbLogin ??

Also check your libs. Probably it's a libq -linker which is missing .

Try to include include/postgresql -L/lib -lpq while you are building your project.

Upvotes: 0

Adri C.S.
Adri C.S.

Reputation: 3007

From the 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.

Try to pass the -lpq flag and see if those other errors you mention disappear.

Upvotes: 3

Related Questions