How to use pkg-config to link a library statically

Kornel picture Kornel · Dec 21, 2014 · Viewed 13.6k times · Source

I'd like to link libpng found by pkg-config statically.

pkg-config --libs --static libpng

outputs

-L/usr/local/Cellar/libpng/1.6.15/lib -lpng16 -lz

I have both libpng16.a libpng16.dylib in that directory, and if I use these flags the library gets linked dynamically.

How can I tell either pkg-config or the linker (preferably in some portable-ish way) that I really want it linked statically?

I've tried adding -static before pkg-config's flags, but that makes clang's ld try and fail to link "crt0.o".

Answer

Quip Yowert picture Quip Yowert · Dec 21, 2014

Try:

-L/usr/local/Cellar/libpng/1.6.15/lib -l:libpng16.a -lz

Using -l with a : character allows you to specify the filename extension.

The -l: option is documented in the GNU ld 2.24 manual:

-l namespec

--library=namespec

Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.

On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.