Skip to content
Search

Pkg-config

lastmod 2024-10-24created 2014-09-286 edits

pkg-config is computer software that provides a unified interface for querying installed libraries for the purpose of compiling software from its source code. pkg-config was originally designed for Linux, but it is now also available for various BSDs, Microsoft Windows, OS X and Solaris.

It outputs various information about installed libraries. This information includes:

  • Parameters for C and C++ compilers
  • Parameters for the linker
  • Package version

The first implementation was written in shell, and was later rewritten in C using the GLib library.

How to check a package version

For example, to check the detailed version of glib-2.0:

pkg-config --modversion glib-2.0

Review

gcc basically takes its arguments on the command line. In other words, it does not read a configuration file and set up includes or libs accordingly. This could make compile settings quite painful, but there is an amazing (actually very simple) program called pkg-config.

All this program does is read a few options and return the matching command-line arguments. That is its only job! But when combined with gcc, it makes for a really convenient compile environment. For example, Ogre's header files are in /usr/local/include and its lib files are in /usr/local/lib. To compile this with gcc, you have to add

-I/usr/local/include/OGRE -L/usr/local/lib -lOgreMain

as options. In practice, no developer remembers all of this. That is why pkg-config was made.

Let's run pkg-config.

$ pkg-config --cflags OGRE --libs OGRE
-I/usr/local/include -I/usr/local/include/OGRE -L/usr/local/lib -lOgreMain

So when using gcc, you can write it as gcc `pkg-config --cflags OGRE --libs OGRE` ....

The files pkg-config uses are .pc files. As for where these pc files are located, they are the files specified in PKG_CONFIG_PATH, and if it is not set, it reportedly looks in /usr/lib/pkgconfig by default. 12

.pc files are also used by the PKG_CHECK_MODULES macro in configure.in (or configure.ac; KDevelop uses configure.in by default), which autoconf uses. For example, the configure.in file of KDevelop for compiling Ogre files looks like this.

AC_INIT(configure.in)

AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(ogretest, 0.1)

AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL

PKG_CHECK_MODULES(OGRE, [OGRE >= 0.15.0])
AM_CXXFLAGS="$AM_CXXFLAGS $OGRE_CFLAGS"
AM_LDFLAGS="$AM_LDFLAGS $OGRE_LIBS"
AC_SUBST(AM_CXXFLAGS, "$AM_CXXFLAGS")
AC_SUBST(AM_LDFLAGS, "$AM_LDFLAGS")

AC_OUTPUT(Makefile src/Makefile)

See also

Favorite site

References


  1. http://kldp.org/node/71409 ↩

  2. As noted in the linked post, pc files in /usr/loca/lib/pkgconfig/ are picked up as well. ↩

author Zer0 sitemap