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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

On Mac, there's a /usr/lib/libpq.dylib (where .dylib is the analogue of .so on Linux and .dll on Windows). The library might be as simple as -lpq therefore.
@JonathanLeffler Correct, it's libpq, just -lpq.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.