Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking to static libraries to build static executable

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.

like image 813
Elaine B Avatar asked Jan 23 '26 02:01

Elaine B


1 Answers

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.

like image 58
Tometzky Avatar answered Jan 26 '26 08:01

Tometzky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!