Blame pkg-config-guide.html

Packit db3073
Packit db3073
Copyright (C) 2010  Dan Nicholson.
Packit db3073
Packit db3073
This program is free software; you can redistribute it and/or modify it
Packit db3073
under the terms of the GNU General Public License as published by the
Packit db3073
Free Software Foundation; either version 2 of the License, or (at your
Packit db3073
option) any later version.
Packit db3073
Packit db3073
This program is distributed in the hope that it will be useful, but
Packit db3073
WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit db3073
General Public License for more details.
Packit db3073
Packit db3073
You should have received a copy of the GNU General Public License along
Packit db3073
with this program; if not, write to the Free Software Foundation, Inc.,
Packit db3073
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit db3073
-->
Packit db3073
Packit db3073
Packit db3073
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Packit db3073
Packit db3073
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
Packit db3073
<head>
Packit db3073
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Packit db3073
  <style type="text/css">
Packit db3073
    pre { background-color: #f0f0f0; padding: 0.4cm; }
Packit db3073
  </style>
Packit db3073
Packit db3073
  <title>Guide to pkg-config</title>
Packit db3073
</head>
Packit db3073
Packit db3073
<body>
Packit db3073
  

Guide to pkg-config

Packit db3073
Packit db3073
  

Dan Nicholson

Packit db3073
Packit db3073
  
    Packit db3073
        
  • Overview
  • Packit db3073
        
  • Why?
  • Packit db3073
        
  • Concepts
  • Packit db3073
        
  • Writing pkg-config files
  • Packit db3073
        
  • Using pkg-config files
  • Packit db3073
        
    Packit db3073
        
  • Frequently asked questions
  • Packit db3073
      
    Packit db3073
    Packit db3073
      

    Overview

    Packit db3073
    Packit db3073
      

    This document aims to give an overview to using the <tt>pkg-config</tt>

    Packit db3073
      tool from the perspective of both a user and a developer. It reviews the
    Packit db3073
      concepts behind <tt>pkg-config</tt>, how to write <tt>pkg-config</tt> files
    Packit db3073
      to support your project, and how to use <tt>pkg-config</tt> to integrate
    Packit db3073
      with 3rd party projects.

    Packit db3073
    Packit db3073
      

    More information on <tt>pkg-config</tt> can be found at the

    Packit db3073
      website and in the
    Packit db3073
      <tt>pkg-config(1)</tt> manual page.

    Packit db3073
    Packit db3073
      

    This document assumes usage of <tt>pkg-config</tt> on a Unix-like

    Packit db3073
      operating system such as Linux. Some of the details may be different on
    Packit db3073
      other platforms.

    Packit db3073
    Packit db3073
      

    Why?

    Packit db3073
    Packit db3073
      

    Modern computer systems use many layered components to provide

    Packit db3073
      applications to the user. One of the difficulties in assembling these parts
    Packit db3073
      is properly integrating them. <tt>pkg-config</tt> collects metadata about
    Packit db3073
      the installed libraries on the system and easily provides it to the user.
    Packit db3073
      

    Packit db3073
    Packit db3073
      

    Without a metadata system such as <tt>pkg-config</tt>, it can be very

    Packit db3073
      difficult to locate and obtain details about the services provided on a
    Packit db3073
      given computer. For a developer, installing <tt>pkg-config</tt> files with
    Packit db3073
      your package greatly eases adoption of your API.

    Packit db3073
    Packit db3073
      

    Concepts

    Packit db3073
    Packit db3073
      

    The primary use of <tt>pkg-config</tt> is to provide the necessary

    Packit db3073
      details for compiling and linking a program to a library. This metadata is
    Packit db3073
      stored in <tt>pkg-config</tt> files. These files have the suffix
    Packit db3073
      <tt>.pc</tt> and reside in specific locations known to the
    Packit db3073
      <tt>pkg-config</tt> tool. This will be described in more detail later.

    Packit db3073
    Packit db3073
      

    The file format contains predefined metadata keywords and freeform

    Packit db3073
      variables. An example may be illustrative:

    Packit db3073
    Packit db3073
    prefix=/usr/local
    Packit db3073
    exec_prefix=${prefix}
    Packit db3073
    includedir=${prefix}/include
    Packit db3073
    libdir=${exec_prefix}/lib
    Packit db3073
    Packit db3073
    Name: foo
    Packit db3073
    Description: The foo library
    Packit db3073
    Version: 1.0.0
    Packit db3073
    Cflags: -I${includedir}/foo
    Packit db3073
    Libs: -L${libdir} -lfoo
    Packit db3073
    Packit db3073
      

    The keyword definitions such as <tt>Name:</tt> begin with a keyword

    Packit db3073
      followed by a colon and the value. The variables such as <tt>prefix=</tt>
    Packit db3073
      are a string and value separated by an equals sign. The keywords are defined
    Packit db3073
      and exported by <tt>pkg-config</tt>. The variables are not necessary, but
    Packit db3073
      can be used by the keyword definitions for flexibility or to store data not
    Packit db3073
      covered by <tt>pkg-config</tt>.

    Packit db3073
    Packit db3073
      

    Here is a short description of the keyword fields. A more in depth

    Packit db3073
      description of these fields and how to use them effectively will be given in
    Packit db3073
      the Writing pkg-config files section.

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

      Writing pkg-config files

      Packit db3073
      Packit db3073
        

      When creating <tt>pkg-config</tt> files for a package, it is first

      Packit db3073
        necessary to decide how they will be distributed. Each file is best used to
      Packit db3073
        describe a single library, so each package should have at least as many
      Packit db3073
        <tt>pkg-config</tt> files as they do installed libraries.

      Packit db3073
      Packit db3073
        

      The package name is determined through the filename of the

      Packit db3073
        <tt>pkg-config</tt> metadata file. This is the portion of the filename prior
      Packit db3073
        to the <tt>.pc</tt> suffix. A common choice is to match the library name to
      Packit db3073
        the <tt>.pc</tt> name. For instance, a package installing <tt>libfoo.so</tt>
      Packit db3073
        would have a corresponding <tt>libfoo.pc</tt> file containing the
      Packit db3073
        <tt>pkg-config</tt> metadata. This choice is not necessary; the <tt>.pc</tt>
      Packit db3073
        file should simply be a unique identifier for your library. Following the
      Packit db3073
        above example, <tt>foo.pc</tt> or <tt>foolib.pc</tt> would probably work
      Packit db3073
        just as well.

      Packit db3073
      Packit db3073
        

      The <tt>Name</tt>, <tt>Description</tt> and <tt>URL</tt> fields are

      Packit db3073
        purely informational and should be easy to fill in. The <tt>Version</tt>
      Packit db3073
        field is a bit trickier to ensure that it is usable by consumers of the
      Packit db3073
        data. <tt>pkg-config</tt> uses the algorithm from
      Packit db3073
        RPM for version comparisons. This works best
      Packit db3073
        with a dotted decimal number such as <tt>1.2.3</tt> since letters can cause
      Packit db3073
        unexpected results. The number should be monotonically increasing and be
      Packit db3073
        as specific as possible in describing the library. Usually it's sufficient
      Packit db3073
        to use the package's version number here since it's easy for consumers to
      Packit db3073
        track.

      Packit db3073
      Packit db3073
        

      Before describing the more useful fields, it will be helpful to

      Packit db3073
        demonstrate variable definitions. The most common usage is to define the
      Packit db3073
        installation paths so that they don't clutter the metadata fields. Since
      Packit db3073
        the variables are expanded recursively, this is very helpful when used in
      Packit db3073
        conjunction with autoconf derived paths.

      Packit db3073
      Packit db3073
      prefix=/usr/local
      Packit db3073
      includedir=${prefix}/include
      Packit db3073
      Packit db3073
      Cflags: -I${includedir}/foo
      Packit db3073
      Packit db3073
        

      The most important <tt>pkg-config</tt> metadata fields are

      Packit db3073
        <tt>Requires</tt>, <tt>Requires.private</tt>, <tt>Cflags</tt>, <tt>Libs</tt>
      Packit db3073
        and <tt>Libs.private</tt>. They will define the metadata used by external
      Packit db3073
        projects to compile and link with the library.

      Packit db3073
      Packit db3073
        

      <tt>Requires</tt> and <tt>Requires.private</tt> define other modules

      Packit db3073
        needed by the library. It is usually preferred to use the private variant of
      Packit db3073
        <tt>Requires</tt> to avoid exposing unnecessary libraries to the program
      Packit db3073
        that is linking with your library. If the program will not be using the
      Packit db3073
        symbols of the required library, it should not be linking directly to that
      Packit db3073
        library. See the discussion of
      Packit db3073
        overlinking for a more
      Packit db3073
        thorough explanation.

      Packit db3073
      Packit db3073
        

      Since <tt>pkg-config</tt> always exposes the link flags of the

      Packit db3073
        <tt>Requires</tt> libraries, these modules will become direct dependencies
      Packit db3073
        of the program. On the other hand, libraries from <tt>Requires.private</tt>
      Packit db3073
        will only be included when static linking. For this reason, it is usually
      Packit db3073
        only appropriate to add modules from the same package in <tt>Requires</tt>.
      Packit db3073
        

      Packit db3073
      Packit db3073
        

      The <tt>Libs</tt> field contains the link flags necessary to use that

      Packit db3073
        library. In addition, <tt>Libs</tt> and <tt>Libs.private</tt> contain link
      Packit db3073
        flags for other libraries not supported by <tt>pkg-config</tt>. Similar to
      Packit db3073
        the <tt>Requires</tt> field, it is preferred to add link flags for external
      Packit db3073
        libraries to the <tt>Libs.private</tt> field so programs do not acquire an
      Packit db3073
        additional direct dependency.

      Packit db3073
      Packit db3073
        

      Finally, the <tt>Cflags</tt> contains the compiler flags for using the

      Packit db3073
        library. Unlike the <tt>Libs</tt> field, there is not a private variant of
      Packit db3073
        <tt>Cflags</tt>. This is because the data types and macro definitions are
      Packit db3073
        needed regardless of the linking scenario.

      Packit db3073
      Packit db3073
        

      Using pkg-config files

      Packit db3073
      Packit db3073
        

      Assuming that there are <tt>.pc</tt> files installed on the system, the

      Packit db3073
        <tt>pkg-config</tt> tool is used to extract the metadata for usage. A short
      Packit db3073
        description of the options can be seen by executing
      Packit db3073
        <tt>pkg-config --help</tt>. A more in depth discussion can be found in the
      Packit db3073
        <tt>pkg-config(1)</tt> manual page. This section will provide a brief
      Packit db3073
        explanation of common usages.</tt>
      Packit db3073
      Packit db3073
        

      Consider a system with two modules, <tt>foo</tt> and <tt>bar</tt>.

      Packit db3073
        Their <tt>.pc</tt> files might look like this:

      Packit db3073
      Packit db3073
      foo.pc:
      Packit db3073
      prefix=/usr
      Packit db3073
      exec_prefix=${prefix}
      Packit db3073
      includedir=${prefix}/include
      Packit db3073
      libdir=${exec_prefix}/lib
      Packit db3073
      Packit db3073
      Name: foo
      Packit db3073
      Description: The foo library
      Packit db3073
      Version: 1.0.0
      Packit db3073
      Cflags: -I${includedir}/foo
      Packit db3073
      Libs: -L${libdir} -lfoo
      Packit db3073
      Packit db3073
      bar.pc:
      Packit db3073
      prefix=/usr
      Packit db3073
      exec_prefix=${prefix}
      Packit db3073
      includedir=${prefix}/include
      Packit db3073
      libdir=${exec_prefix}/lib
      Packit db3073
      Packit db3073
      Name: bar
      Packit db3073
      Description: The bar library
      Packit db3073
      Version: 2.1.2
      Packit db3073
      Requires.private: foo >= 0.7
      Packit db3073
      Cflags: -I${includedir}
      Packit db3073
      Libs: -L${libdir} -lbar
      Packit db3073
      Packit db3073
        

      The version of the modules can be obtained with the <tt>--modversion</tt>

      Packit db3073
        option.

      Packit db3073
      Packit db3073
      $ pkg-config --modversion foo
      Packit db3073
      1.0.0
      Packit db3073
      $ pkg-config --modversion bar
      Packit db3073
      2.1.2
      Packit db3073
      Packit db3073
        

      To print the link flags needed for each module, use the <tt>--libs</tt>

      Packit db3073
        option.

      Packit db3073
      Packit db3073
      $ pkg-config --libs foo
      Packit db3073
      -lfoo
      Packit db3073
      $ pkg-config --libs bar
      Packit db3073
      -lbar
      Packit db3073
      Packit db3073
        

      Notice that <tt>pkg-config</tt> has suppressed part of the <tt>Libs</tt>

      Packit db3073
        field for both modules. This is because it treats the <tt>-L</tt> flag
      Packit db3073
        specially and knows that the <tt>${libdir}</tt> directory <tt>/usr/lib</tt>
      Packit db3073
        is part of the system linker search path. This keeps <tt>pkg-config</tt>
      Packit db3073
        from interfering with the linker operation.

      Packit db3073
      Packit db3073
        

      Also, although <tt>foo</tt> is required by <tt>bar</tt>, the link flags

      Packit db3073
        for <tt>foo</tt> are not output. This is because <tt>foo</tt> is not
      Packit db3073
        directly needed by an application that only wants to use the <tt>bar</tt>
      Packit db3073
        library. For statically linking a <tt>bar</tt> application, we need both
      Packit db3073
        sets of linker flags:

      Packit db3073
      Packit db3073
      $ pkg-config --libs --static bar
      Packit db3073
      -lbar -lfoo
      Packit db3073
      Packit db3073
        

      <tt>pkg-config</tt> needs to output both sets of link flags in this case

      Packit db3073
        to ensure that the statically linked application will find all the necessary
      Packit db3073
        symbols. On the other hand, it will always output all the <tt>Cflags</tt>.
      Packit db3073
        

      Packit db3073
      Packit db3073
      $ pkg-config --cflags bar
      Packit db3073
      -I/usr/include/foo
      Packit db3073
      $ pkg-config --cflags --static bar
      Packit db3073
      -I/usr/include/foo
      Packit db3073
      Packit db3073
        

      Another useful option, <tt>--exists</tt>, can be used to test for a

      Packit db3073
        module's availability.

      Packit db3073
      Packit db3073
      $ pkg-config --exists foo
      Packit db3073
      $ echo $?
      Packit db3073
      0
      Packit db3073
      Packit db3073
        

      One of the nicest features of <tt>pkg-config</tt> is providing version

      Packit db3073
        checking. It can be used to determine if a sufficient version is available.
      Packit db3073
        

      Packit db3073
      Packit db3073
      $ pkg-config --libs "bar >= 2.7"
      Packit db3073
      Requested 'bar >= 2.7' but version of bar is 2.1.2
      Packit db3073
      Packit db3073
        

      Some commands will provide more verbose output when combined with the

      Packit db3073
        <tt>--print-errors</tt> option.

      Packit db3073
      Packit db3073
      $ pkg-config --exists --print-errors xoxo
      Packit db3073
      Package xoxo was not found in the pkg-config search path.
      Packit db3073
      Perhaps you should add the directory containing `xoxo.pc'
      Packit db3073
      to the PKG_CONFIG_PATH environment variable
      Packit db3073
      No package 'xoxo' found
      Packit db3073
      Packit db3073
        

      The message above references the <tt>PKG_CONFIG_PATH</tt> environment

      Packit db3073
        variable. This variable is used to augment <tt>pkg-config</tt>'s search
      Packit db3073
        path. On a typical Unix system, it will search in the directories
      Packit db3073
        <tt>/usr/lib/pkgconfig</tt> and <tt>/usr/share/pkgconfig</tt>. This will
      Packit db3073
        usually cover system installed modules. However, some local modules may be
      Packit db3073
        installed in a different prefix such as <tt>/usr/local</tt>. In that case,
      Packit db3073
        it's necessary to prepend the search path so that <tt>pkg-config</tt> can
      Packit db3073
        locate the <tt>.pc</tt> files.

      Packit db3073
      Packit db3073
      $ pkg-config --modversion hello
      Packit db3073
      Package hello was not found in the pkg-config search path.
      Packit db3073
      Perhaps you should add the directory containing `hello.pc'
      Packit db3073
      to the PKG_CONFIG_PATH environment variable
      Packit db3073
      No package 'hello' found
      Packit db3073
      $ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
      Packit db3073
      $ pkg-config --modversion hello
      Packit db3073
      1.0.0
      Packit db3073
      Packit db3073
        

      A few autoconf macros

      Packit db3073
        are also provided to ease integration of <tt>pkg-config</tt> modules into
      Packit db3073
        projects.

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

        Frequently asked questions

        Packit db3073
        Packit db3073
          
          Packit db3073
              
        1. My program uses library <tt>x</tt>. What do I do?
        2. Packit db3073
          Packit db3073
              

          The <tt>pkg-config</tt> output can easily be used on the compiler

          Packit db3073
              command line. Assuming the <tt>x</tt> library has a <tt>x.pc</tt>
          Packit db3073
              <tt>pkg-config</tt> file:

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

          The integration can be more robust when used with

          Packit db3073
              autoconf and
          Packit db3073
              automake. By using the
          Packit db3073
              supplied <tt>PKG_CHECK_MODULES</tt> macro, the metadata is easily accessed
          Packit db3073
              in the build process.

          Packit db3073
          Packit db3073
          configure.ac:
          Packit db3073
          PKG_CHECK_MODULES([X], [x])
          Packit db3073
          Packit db3073
          Makefile.am:
          Packit db3073
          myapp_CFLAGS = $(X_CFLAGS)
          Packit db3073
          myapp_LDADD = $(X_LIBS)
          Packit db3073
          Packit db3073
              

          If the <tt>x</tt> module is found, the macro will fill and substitute

          Packit db3073
              the <tt>X_CFLAGS</tt> and <tt>X_LIBS</tt> variables. If the module is not
          Packit db3073
              found, an error will be produced. Optional 3rd and 4th arguments can be
          Packit db3073
              supplied to <tt>PKG_CHECK_MODULES</tt> to control actions when the module
          Packit db3073
              is found or not.

          Packit db3073
          Packit db3073
              
        3. My library <tt>z</tt> installs header files which include <tt>libx</tt>
        4. Packit db3073
              headers. What do I put in my <tt>z.pc</tt> file?
          Packit db3073
          Packit db3073
              

          If the <tt>x</tt> library has <tt>pkg-config</tt> support, add it to

          Packit db3073
              the <tt>Requires.private</tt> field. If it does not, augment the
          Packit db3073
              <tt>Cflags</tt> field with the necessary compiler flags for using the
          Packit db3073
              <tt>libx</tt> headers. In either case, <tt>pkg-config</tt> will output
          Packit db3073
              the compiler flags when <tt>--static</tt> is used or not.

          Packit db3073
          Packit db3073
              
        5. My library <tt>z</tt> uses <tt>libx</tt> internally, but does not
        6. Packit db3073
              expose <tt>libx</tt> data types in its public API. What do I put in my
          Packit db3073
              <tt>z.pc</tt> file?
          Packit db3073
          Packit db3073
              

          Again, add the module to <tt>Requires.private</tt> if it supports

          Packit db3073
              <tt>pkg-config</tt>. In this case, the compiler flags will be emitted
          Packit db3073
              unnecessarily, but it ensures that the linker flags will be present when
          Packit db3073
              linking statically. If <tt>libx</tt> does not support <tt>pkg-config</tt>,
          Packit db3073
              add the necessary linker flags to <tt>Libs.private</tt>.

          Packit db3073
            
          Packit db3073
          Packit db3073
            
          Packit db3073
          Packit db3073
            <address>Dan Nicholson <dbn.lists (at) gmail (dot) com></address>
          Packit db3073
          Packit db3073
            

          Copyright (C) 2010 Dan Nicholson.

          Packit db3073
            This document is licensed under the
          Packit db3073
            GNU General Public License, Version 2
          Packit db3073
            or any later version.

          Packit db3073
          Packit db3073
          </body>
          Packit db3073
          </html>