0

I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>

int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621  sslmode=require user=ggales password=1234");

if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");


return 0;
}

When I run it, I get the following error:

/tmp/cc73kO0N.o: In function `main':
main.c:(.text+0x15): undefined reference to `PQconnectdb'
main.c:(.text+0x25): undefined reference to `PQstatus'
main.c:(.text+0x40): undefined reference to `PQfinish'
main.c:(.text+0x5d): undefined reference to `PQfinish'
collect2: error: ld returned 1 exit status

This is strange, as PQconnectdb etc are all functions that are defined in libpq-fe.h, which I have already included in the code.

Any help would be great thanks.

user1974753
  • 1,359
  • 1
  • 18
  • 32

1 Answers1

2

#include <libpq-fe.h> does not link to the library, it only includes information about the functions and data types that the library provides.

You must tell the linker where the references that are declared in libpq-fe.h can actually be found.

If you are using a Makefile to compile you code you should add -lpq to your LDFLAGS or linking command.

Post the command you are running to compile to give us more information.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
Jonatan Goebel
  • 1,107
  • 9
  • 14