I am trying to build a static executable of a C program which requires the following libraries from the Postgresql libecpg-dev package: libecpg, libpq, and libpgtypes. The static libraries, libecpg.a, libpq.a, libpgtypes.a, are all located in the same place as their dynamic versions, /usr/lib. I have tried adding the -L option as their path anyway, but I get the same result.
My executable needs to be static so that the binary can be portable so that users will not have to install the libraries onto their systems to use it. The program builds fine when I use the following command with the dynamic libraries: (The -I directory has to be included for some of the Postgresql functions I'm using)
gcc main.o -lecpg -lpq -lpgtypes -I/usr/include/postgresql -o program_name
When I try to link to the static libraries, I am using the following command:
gcc -static main.o -lecpg -lpq -lpgtypes -I/usr/include/postgresql -o program_name
The compiler, however, gives me a long list of "undefined reference" errors as output. There are so many, but here is a small sampling:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o):
In function `get_descriptors':
(.text+0xc3): undefined reference to `pthread_once'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `ECPGdeallocate_desc':
(.text+0x2c6): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `ECPGallocate_desc':
(.text+0x376): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libecpg.a(descriptor.o): In function `get_descriptors':
(.text+0xd2): undefined reference to `pthread_getspecific'
When I change the order as follows:
gcc -I/usr/include/postgresql -static -lecpg -lpq -lpgtypes -o program_name main.o
the compiler output instead looks like the following (once again, just a small sampling):
main.c:(.text+0x5c6): undefined reference to `ECPGconnect'
main.c:(.text+0x83e): undefined reference to `ECPGdo'
main.c:(.text+0x843): undefined reference to `ECPGget_sqlca'
main.c:(.text+0x85c): undefined reference to `ECPGdisconnect'
main.c:(.text+0x87c): undefined reference to `ECPGget_sqlca'
I have tried using the -I option all around (along with all of the other options), but nothing seems to work.
I think you just can't create a fully static non-trivial executable in modern Linux distribution anymore. But you can statically include some specific libraries.