gccldpkg-config

Is it possible to use an environment variable in a pkg-config.pc file?


I'm trying to use a library libfoo.so that will be installed in $(LIBDIR)/foo/ where $(LIBDIR) is an environment variable that can be different based on the build setup.

In order for other components to correctly link with this library, they need to have the correct LDFLAGS set, namely they need to have -L$(LIBDIR)/foo -lfoo set. In order to make this a bit more generic, I would like to add a libfoo.pc file to libfoo with these flags set. My file currently has the following contents:

Name: libfoo
Description: Example library
Version: 1.0.0
Libs: -L$(LIBDIR)/foo -lfoo

My other component which uses libfoo has the following simplified makefile:

all:
    $(CC) bar.c -o out $(shell pkg-config --libs libfoo)

The library is correctly found by pkg-config, but running make gives the following error:

$ make
cc bar.c -o out -L$(LIBDIR)/foo -lfoo
/bin/sh: LIBDIR: command not found

In other words, $(LIBDIR) is not expanded into the value that is set as an environment variable. Is it possible to use environment variables or makefile variables in .pc files?


Solution

  • Use --define-variable.

    Makefile:

        $(CC) bar.c -o out $(shell pkg-config --define-variable=LIBDIR=$(LIBDIR) --libs libfoo)
    

    libfoo.pc:

    Libs: -L${LIBDIR}/foo -lfoo