Friday, June 27, 2008

gcc: statically linked shared library

Usually it's painful to distribute binaries of shared libraries across different linux distributions. They have different set of versions of shared libraries. For example if you have a library that uses to communicate with PostgreSQL you will fail to link against your library on distribution that has But there is no difference for you which to use: or, anyway you will be able to connect to PostgreSQL and do DB related stuff. There is an option to compile a shared library with statically linked shared libraries in it. Let's try to build library that uses PostgreSQL's libpq: At first you just create objects for you library:

gcc -fPIC test.c -c
and compile shared library:
gcc test.o -o -static-libgcc -shared -Wl,\
-Wl,-static -lpq -lssl -lcrypt -lcrypto -lz
-static-libgcc is needed to link against libgcc statically[not all versions of gcc need that]. Shared libraries should go after -Wl,-static option. Be aware that you have to specify all shared libraries that can be involved on linking stage, otherwise they should be specified for binary that will be linked against libtest. Here I specified libpq, libssl, libcrypt, libcrypto, libz, to be linked statically. libpq also depends on krb5, but there's no static version of it in my distribution, so I'll link it to final binary. ldd output:
 statically linked
Now you can link your binary against libtest:
g++ -o usetest -L./ -ltest -lkrb5
That's all. ldd shows:
ldd usetest =>  (0xb7f37000) => /tmp/work/ (0xb7c37000) => /usr/lib/ (0xb7bad000) => /usr/lib/ (0xb7aca000) => /lib/ (0xb7aa4000) => /lib/ (0xb7964000) => /usr/lib/ (0xb7940000) => /lib/ (0xb793c000) => /usr/lib/ (0xb7934000) => /lib/ (0xb7921000) => /usr/lib/ (0xb7916000)
 /lib/ (0xb7f38000) => /lib/ (0xb7912000)
No libpq or libssl, libcrypt, libcrypto, libz. Congratulations. As far as I know, not all versions of ld support this. I succeeded with 'GNU ld version 20061020' and 'GNU ld (GNU Binutils) 2.18', but failed with 'GNU ld version 2.16.1'[on x86_64]

No comments: