Using pkg-config to set variables

When compiling software from source you might need to set variables such as CFLAGS or LDFLAGS. Here the pkg-config command comes handy. Nevertheless, pkg-config only works with libraries supporting pkg-config, meaning that there is a /pkgconfig sub-directory in the library directory with a corresponding *.pc file. If that is the case you can easily set these variables by typing (bash):

export CFLAGS="`pkg-config --cflags PACKAGE-NAME`"


export LDLAGS="`pkg-config --libs PACKAGE-NAME`"

where PACKAGE-NAME is the package you need. However, by default pkg-config looks for *.pc files in system defined paths only. If you need pkg-config to search another path as well you may set the PKG_CONFIG_PATH variable accordingly. This variable contain paths leading to a directory containing *.pc' files. A package might come with a pkg-config directory containing the corresponding *.pc files. You may set multiple paths in the PKG_CONFIG_PATH variable using colon (:) as separator.

Here is an example setting two additional paths:

export PKG_CONFIG_PATH="path1-to-*.pc-files:path2-to-*.pc-files"

It is important to note that the --libs option sometimes only returns the link time directory, omitting the run time directory. Therefore, you might have to correct the LDFLAGS variable accordingly.

Remark: Paths contained in PKG_CONFIG_PATH are preferred over the default path by pkg-config. This means if a corresponding *.pc file is found in the PKG_CONFIG_PATH directories, a *.pc with the same name for instance in /usr/lib/pkgconfig will not be considered.

Here is an example:

export CFLAGS="`pkg-config --cflags gtk+-2.0`"

More details can be found in the pkg-config(1) manpage.


Workstations/Linux/Applications/PKGConfig (last edited 2020-09-10 09:22:14 by bonaccos)