Search code examples
buildstatic-linkingpkg-config

writing a pkg-config file that supports `--static`


I want to provide a pkg-config file that (additionally) supports static linking.

Usually you can do that with something like:

Libs: -L${libdir} -lfoo
Libs.private: -L${libdir} -lbar
Cflags: -I${includedir} -DFOO

which will yield:

$ pkg-config --cflags foo
-I/usr/local/include -DFOO
$ pkg-config --libs foo
-L/usr/local/lib -lfoo
$ pkg-config --static --cflags foo
-I/usr/local/include -DFOO
$ pkg-config --static --libs foo
-L/usr/local/lib -lfoo -lbar

Unfortunately, my library foo requires to use different pre-processor defines when linking dynamically/statically and also to link in a different version of the lib.

So the desired output would be:

$ pkg-config --cflags foo
-I/usr/local/include -DFOO_DYNAMIC
$ pkg-config --libs foo
-L/usr/local/lib -lfoo
$ pkg-config --static --cflags foo
-I/usr/local/include -DFOO_STATIC
$ pkg-config --static --libs foo
-L/usr/local/lib -lfoo_static

So the question is:

  • is it possible to provide different CFLAGS/LIBS for the static variant rather than just adding on top of the dynamic variant?

Solution

  • Short answer: No.

    A workaround is to provide a different .pc file, named foo-static.pc or something similar.