pkg-config Notes

Table of Contents

Concepts1

The file format contains predefined metadata keywords and freeform variables. An example may be illustrative:

prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: foo
Description: The foo library
Version: 1.0.0
Cflags: -I${includedir}/foo
Libs: -L${libdir} -lfoo

Here is a short description of the keyword fields. A more in depth description of these fields and how to use them effectively will be given in the Writing pkg-config files section.

  • Name: A human-readable name for the library or package. This does not affect usage of the pkg-config tool, which uses the name of the .pc file.
  • Description: A brief description of the package.
  • URL: An URL where people can get more information about and download the package.
  • Version: A string specifically defining the version of the package.
  • Requires: A list of packages required by this package. The versions of these packages may be specified using the comparison operators =, <, >, <= or >=.
  • Requires.private: A list of private packages required by this package but not exposed to applications. The version specific rules from the Requires field also apply here.
  • Conflicts: An optional field describing packages that this one conflicts with. The version specific rules from the Requires field also apply here. This field also takes multiple instances of the same package. E.g., Conflicts: bar < 1.2.3, bar >= 1.3.0.
  • Cflags: The compiler flags specific to this package and any required libraries that don't support pkg-config. If the required libraries support pkg-config, they should be added to Requires or Requires.private.
  • Libs: The link flags specific to this package and any required libraries that don't support pkg-config. The same rule as Cflags applies here.
  • Libs.private: The link flags for private libraries required by this package but not exposed to applications. The same rule as Cflags applies here.

Using pkg-config files

A few autoconf macros are also provided to ease integration of pkg-config modules into projects.

  • PKG_PROG_PKG_CONFIG([MIN-VERSION]) : Locates the pkg-config tool on the system and checks the version for compatibility.
  • PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) : Checks to see whether a particular set of modules exists.
  • PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) : Checks to see whether a particular set of modules exists. If so, it sets <VARIABLE-PREFIX>CFLAGS and <VARIABLE-PREFIX>LIBS according to the output from pkg-config –cflags and pkg-config –libs.

use example

My program uses library x. What do I do? The pkg-config output can easily be used on the compiler command line. Assuming the x library has a x.pc pkg-config file:

cc `pkg-config --cflags --libs x` -o myapp myapp.c

The integration can be more robust when used with autoconf and automake. By using the supplied PKG_CHECK_MODULES macro, the metadata is easily accessed in the build process.

configure.ac:
PKG_CHECK_MODULES([X], [x])

Makefile.am:
myapp_CFLAGS = $(X_CFLAGS)
myapp_LDADD = $(X_LIBS)

Footnotes:

Author: Shi Shougang

Created: 2015-03-05 Thu 23:20

Emacs 24.3.1 (Org mode 8.2.10)

Validate