-1

I am on Debian 10 and I want to develop an C application for PostgreSQL. This is why I installed a packages postgresql and libpq-dev. Later clearly installed header files inside the system folders:

┌───┐
│ $ │ ziga > ziga--workstation > 001--hello_world
└─┬─┘
  └─> find /usr/include/ | grep libpq
/usr/include/postgresql/libpq-fe.h
/usr/include/postgresql/libpq
/usr/include/postgresql/libpq/libpq-fs.h
/usr/include/postgresql/libpq-events.h
/usr/include/postgresql/internal/libpq
/usr/include/postgresql/internal/libpq/pqcomm.h
/usr/include/postgresql/internal/libpq-int.h

But when I compile this simple C program:

#include <stdio.h>
#include <postgresql/libpq-fe.h>

int main(int argc, char * argv[]){

    int v = PQlibVersion();

    printf("Version of libpq: %d\n", v);

    return 0;
}

compiler says:

gcc -std=c17 -Wall -Wpedantic -g -gdwarf-2 -o main.elf main.c
/usr/bin/ld: /tmp/ccVzig6T.o: in function `main':
/home/ziga/Dropbox/workspace/racunalnistvo/projects--pistam/2021-03-03--postgressql_c/001--hello_world/main.c:6: undefined reference to `PQlibVersion'
collect2: error: ld returned 1 exit status
make: *** [makefile:59: main.elf] Error 1

I don't understand. Header files are inside /usr/include where they should be found... Is library not being found or what? It is also installed...

┌───┐
│ $ │ ziga > ziga--workstation > 001--hello_world
└─┬─┘
  └─> find /usr/lib | grep libpq
/usr/lib/x86_64-linux-gnu/libpq.a
/usr/lib/x86_64-linux-gnu/libpq.so
/usr/lib/x86_64-linux-gnu/libpq.so.5
/usr/lib/x86_64-linux-gnu/pkgconfig/libpq.pc
/usr/lib/x86_64-linux-gnu/libpq.so.5.11
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqsignal.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-secure-openssl.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/auth.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqcomm.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-fsstubs.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqformat.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/ifaddr.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-secure.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/pqmq.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/crypt.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/be-secure-common.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/hba.bc
/usr/lib/postgresql/11/lib/bitcode/postgres/libpq/auth-scram.bc
/usr/lib/postgresql/11/lib/libpqwalreceiver.so

Can anyone tell me how to best solve this?

71GA
  • 1,132
  • 6
  • 36
  • 69
  • Ah never mind. I added `-lpq` to the compiler command... Idiot... – 71GA Mar 03 '21 at 11:21
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user7860670 Mar 03 '21 at 11:22

1 Answers1

1

As the log says, The linker is unable to find any reference to PQlibVersion. Since the linker is running, it clearly is not a problem of compiling but linking.

/usr/bin/ld: /tmp/ccVzig6T.o: in function `main':
/home/ziga/Dropbox/workspace/racunalnistvo/projects--pistam/2021-03-03--postgressql_c/001--hello_world/main.c:6: undefined reference to `PQlibVersion'
collect2: error: ld returned 1 exit status

I think you should add linker options to link against the postgress library. Add -lpq option to you compiler arguments. For a more complete explanation, please read Building libpq Programs.