need help producing statically linked executable

Andi Hellmund
Tue Aug 3 22:22:00 GMT 2010

Hey Charlie

> Thanks. I knew that ldd would list the static libraries.

ldd doesn't really list the static libraries, but it lists the depending 
shared libraries of an executable/shared libraries (as for example found 
in the readelf -d output of an ELF executable/shared library)

> What I am looking for is an automated way to transform
> the dynamic linking command into the static link command.
> I guess I can parse the output of ldd through a perl script
> that generates "-llinux-vdso -lidn ...". But this seems so
> primitive. Isn't there an elegant way to ask the linker
> to do that work for you?

I don't know/doubt if there's any solution by using the gcc driver and 
the -static option. But, maybe the pkg-config utility will do the work 
for you. It allows you to manually specify all the library dependencies 
(as returned by the ldd output) and then easily include all these 
depending libraries into your build. Check out the pkg-config(1) man page.

Some libraries might also be shipped with a pre-configured pkg-config 
file *.pc. For example, my distro includes a pre-configured file for 
libcurl under /usr/lib/pkgconf/libcurl.pc. If I then use

pkg-config --static --libs libcurl

I get the following output:

-lcurl -lidn -llber -lldap -lrt -lgssapi_krb5 -lz -lgnutls -lgcrypt

This might not be sufficient because some of the libraries are missing, 
but you could easily extend this package or create a new one.

You could then include this into your build as follows:

gcc <your_options> -static `pgk-config --static --libs libcurl`

I hope that helps at least a little bit ...

Best regards,

More information about the Gcc-help mailing list