pkg-config(1)

πŸ“¦ version 0.28, in centos-stream 9

πŸŽ€ pkg-config(1) πŸŽ€

General Commands Manual β€” yes bestie, that's the manual name, and this whole page is about pkg-config(1). Buckle up πŸ’…


✨ NAME ✨

pkg-config β€” Return metainformation about installed libraries. It's giving "librarian but make it command line" πŸ“šβœ¨


πŸ¦‹ SYNOPSIS πŸ¦‹

pkg-config [--modversion] [--version] [--help] [--print-errors]
[--silence-errors] [--errors-to-stdout] [--debug] [--cflags] [--libs]
[--libs-only-L] [--libs-only-l] [--cflags-only-I]
[--variable=VARIABLENAME] [--define-variable=VARIABLENAME=VARIABLEVALUE]
[--print-variables] [--uninstalled] [--exists] [--atleast-
version=VERSION] [--exact-version=VERSION] [--max-version=VERSION]
[--list-all] [LIBRARIES...]  [--print-provides] [--print-requires]
[--print-requires-private] [LIBRARIES...]

πŸ’– DESCRIPTION πŸ’–

Okay so the pkg-config program is used to retrieve information about installed libraries in the system. It is typically used to compile and link against one or more libraries. Here is a typical usage scenario in a Makefile:

program: program.c
     cc program.c $(pkg-config --cflags --libs gnomeui)

pkg-config retrieves information about packages from special metadata files. These files are named after the package, and has a .pc extension. On most systems, pkg-config looks in /usr/lib/pkgconfig, /usr/share/pkgconfig, /usr/local/lib/pkgconfig and /usr/local/share/pkgconfig for these files. It will additionally look in the colon-separated (on Windows, semicolon-separated) list of directories specified by the PKG_CONFIG_PATH environment variable. That's the search path, bestie β€” write it down. 🫢

The package name specified on the pkg-config command line is defined to be the name of the metadata file, minus the .pc extension. If a library can install multiple versions simultaneously, it must give each version its own name (for example, GTK 1.2 might have the package name gtk+ while GTK 2.0 has gtk+-2.0). Version drama, but organized. πŸ’…

In addition to specifying a package name on the command line, the full path to a given .pc file may be given instead. This allows a user to directly query a particular .pc file. Direct query, no middleman β€” iconic. ✨


πŸ’… OPTIONS πŸ’…

The following options are supported:

  • --modversion β€” Requests that the version information of the libraries specified on the command line be displayed. If pkg-config can find all the libraries on the command line, each library's version string is printed to stdout, one version per line. In this case pkg-config exits successfully. If one or more libraries is unknown, pkg-config exits with a nonzero code, and the contents of stdout are undefined. Undefined, bestie β€” not a vibe, just facts. πŸ’€
  • --version β€” Displays the version of pkg-config and terminates. Short, sweet, done. ✨
  • --help β€” Displays a help message and terminates. The classic "help me bestie" button. 🫢
  • --print-errors β€” If one or more of the modules on the command line, or their dependencies, are not found, or if an error occurs in parsing a .pc file, then this option will cause errors explaining the problem to be printed. With "predicate" options such as --exists pkg-config runs silently by default, because it's usually used in scripts that want to control what's output. This option can be used alone (to just print errors encountered locating modules on the command line) or with other options. The PKG_CONFIG_DEBUG_SPEW environment variable overrides this option. Errors: on. πŸ”₯
  • --silence-errors β€” If one or more of the modules on the command line, or their dependencies, are not found, or if an error occurs in parsing a .pc file, then this option will keep errors explaining the problem from being printed. With "predicate" options such as --exists pkg-config runs silently by default, because it's usually used in scripts that want to control what's output. So this option is only useful with options such as --cflags or --modversion that print errors by default. The PKG_CONFIG_DEBUG_SPEW environment variable overrides this option. Errors: shhh. πŸ™ˆ
  • --errors-to-stdout β€” If printing errors, print them to stdout rather than the default stderr. Same drama, different stream. 🌈
  • --debug β€” Print debugging information. This is slightly different than the PKG_CONFIG_DEBUG_SPEW environment variable, which also enable --print-errors. Debug mode: enter the group chat logs. 🧚

The following options are used to compile and link programs:

  • --cflags β€” This prints pre-processor and compile flags required to compile the packages on the command line, including flags for all their dependencies. Flags are "compressed" so that each identical flag appears only once. pkg-config exits with a nonzero code if it can't find metadata for one or more of the packages on the command line. Duplicate flags? Not on her watch. πŸ’…
  • --cflags-only-I β€” This prints the -I part of --cflags. That is, it defines the header search path but doesn't specify anything else. Just the -I, bestie. πŸŽ€
  • --libs β€” This option is identical to --cflags, only it prints the link flags. As with --cflags, duplicate flags are merged (maintaining proper ordering), and flags for dependencies are included in the output. Link flags, merged and ordered β€” slay. πŸ”₯
  • --libs-only-L β€” This prints the -L/-R part of --libs. That is, it defines the library search path but doesn't specify which libraries to link with. Search path only, no library names. ✨
  • --libs-only-l β€” This prints the -l part of --libs for the libraries specified on the command line. Note that the union of --libs-only-l and --libs-only-L may be smaller than --libs, due to flags such as -rdynamic. Important: union can be smaller β€” no cap. πŸ’€
  • --variable=VARIABLENAME β€” This returns the value of a variable defined in a package's .pc file. Most packages define the variable prefix, for example, so you can say:
$ pkg-config --variable=prefix glib-2.0
/usr/
  • --define-variable=VARIABLENAME=VARIABLEVALUE β€” This sets a global value for a variable, overriding the value in any .pc files. Most packages define the variable prefix, for example, so you can say:
$ pkg-config --print-errors --define-variable=prefix=/foo \
             --variable=prefix glib-2.0
/foo
  • --print-variables β€” Returns a list of all variables defined in the package. Every variable, exposed. πŸ‘‘
  • --uninstalled β€” Normally if you request the package "foo" and the package "foo-uninstalled" exists, pkg-config will prefer the "-uninstalled" variant. This allows compilation/linking against uninstalled packages. If you specify the --uninstalled option, pkg-config will return successfully if any "-uninstalled" packages are being used, and return failure (false) otherwise. (PKG_CONFIG_DISABLE_UNINSTALLED environment variable keeps pkg-config from implicitly choosing "-uninstalled" packages, so if that variable is set, they will only have been used if you pass a name like foo-uninstalled on the command line explicitly.) Uninstalled variant: main character energy. 🌸
  • --exists, --atleast-version=VERSION, --exact-version=VERSION, --max-version=VERSION β€” These options test whether the package or list of packages on the command line are known to pkg-config, and optionally whether the version number of a package meets certain constraints. If all packages exist and meet the specified version constraints, pkg-config exits successfully. Otherwise it exits unsuccessfully. Only the first VERSION comparing option will be honored. Subsequent options of this type will be ignored. First version check wins β€” periodt. πŸ’…

Rather than using the version-test options, you can simply give a version constraint after each package name, for example:

$ pkg-config --exists 'glib-2.0 >= 1.3.4 libxml = 1.8.3'

Remember to use --print-errors if you want error messages. When no output options are supplied to pkg-config, --exists is implied. No output options? --exists said "I got this." ✨

  • --msvc-syntax β€” This option is available only on Windows. It causes pkg-config to output -l and -L flags in the form recognized by the Microsoft Visual C++ command-line compiler, cl. Specifically, instead of -Lx:/some/path it prints /libpath:x/some/path, and instead of -lfoo it prints foo.lib. Note that the --libs output consists of flags for the linker, and should be placed on the cl command line after a /link switch. Windows bestie, this one's for you. πŸͺŸ
  • --dont-define-prefix β€” This option is available only on Windows. It prevents pkg-config from automatically trying to override the value of the variable prefix in each .pc file. Hands off the prefix, Windows. 🫢
  • --prefix-variable=PREFIX β€” Also this option is available only on Windows. It sets the name of the variable that pkg-config automatically sets as described above. Rename the auto-set variable? Cute. πŸŽ€
  • --static β€” Output libraries suitable for static linking. That means including any private libraries in the output. This relies on proper tagging in the .pc files, else a too large number of libraries will ordinarily be output. Proper tagging or chaos β€” no in-between. πŸ’€
  • --list-all β€” List all modules found in the pkg-config path. Full roster, exposed. πŸ“‹βœ¨
  • --print-provides β€” List all modules the given packages provides. What it provides: listed. πŸ‘‘
  • --print-requires β€” List all modules the given packages requires. What it requires: listed. 🌈
  • --print-requires-private β€” List all modules the given packages requires for static linking (see --static). Private requires, for static linking β€” iykyk. 🧚

🌈 ENVIRONMENT VARIABLES 🌈

okay bestie, these are the env vars that boss pkg-config around. set them, don't set them, that's your vibe check πŸ’…

  • PKG_CONFIG_PATH β€” a colon-separated list (semicolon-separated on Windows, ikr) of directories to go hunting for .pc files in. the default directory is always searched after searching the path, no matter what you do β€” and that default is libdir/pkgconfig:datadir/pkgconfig, where libdir is the libdir for pkg-config and datadir is the datadir for pkg-config back when it got installed ✨
  • PKG_CONFIG_DEBUG_SPEW β€” set this and pkg-config goes full detective mode: it prints all kinds of debugging information and reports all errors. literally the "tell me everything" button πŸ•΅οΈβ€β™€οΈ
  • PKG_CONFIG_TOP_BUILD_DIR β€” a value to set for the magic variable pc_top_builddir, which may show up in .pc files. if this env var isn't set, the default value '$(top_builddir)' gets used instead. this variable should refer to the top builddir of the Makefile where the compile/link flags that pkg-config reports are gonna be used. it only matters when you're compiling/linking against a package that hasn't been installed yet β€” so like, pre-glow-up package energy πŸ›βž‘οΈπŸ¦‹
  • PKG_CONFIG_DISABLE_UNINSTALLED β€” so normally, if you ask for the package foo and a package called foo-uninstalled also exists, pkg-config is gonna prefer the -uninstalled variant. that's the whole point: it lets you compile/link against uninstalled packages. set this env var and it disables that behavior. no cap.
  • PKG_CONFIG_ALLOW_SYSTEM_CFLAGS β€” don't strip -I/usr/include out of cflags. that's it, that's the whole job πŸŽ€
  • PKG_CONFIG_ALLOW_SYSTEM_LIBS β€” don't strip -L/usr/lib or -L/lib out of libs. same deal, different flag πŸ’«
  • PKG_CONFIG_SYSROOT_DIR β€” modifies -I and -L so they use the directories located in the target sysroot. this option is so useful when you're cross-compiling packages that use pkg-config to figure out CFLAGS and LDFLAGS. -I and -L get modified to point at the new system root β€” meaning a -I/usr/include/libfoo becomes -I/var/target/usr/include/libfoo when PKG_CONFIG_SYSROOT_DIR equals /var/target (same rule applies to -L). iconic cross-compile slay πŸ”₯
  • PKG_CONFIG_LIBDIR β€” replaces the default pkg-config search directory, which is usually /usr/lib/pkgconfig. you're the main character now πŸ‘‘

πŸ” QUERYING PKG-CONFIG'S DEFAULTS πŸ”

fun fact: pkg-config can be used to query itself for the default search path, the version number, and other info. it's giving self-aware queen πŸ‘‘ for instance:

$ pkg-config --variable pc_path pkg-config

or

$ pkg-config --modversion pkg-config

πŸͺŸ WINDOWS SPECIALITIES πŸͺŸ

so here's the Windows lore 🧚:

if a .pc file is found in a directory that matches the usual conventions (i.e., it ends with \lib\pkgconfig or \share\pkgconfig), then the prefix for that package is assumed to be the grandparent of the directory where the file was found β€” and the prefix variable gets overridden for that file accordingly ✨

and if the value of a variable in a .pc file begins with the original, non-overridden, value of the prefix variable, then the overridden value of prefix is used instead. got it? slay πŸ’€


πŸ› οΈ AUTOCONF MACROS πŸ› οΈ

PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES [,ACTION-IF-FOUND [,ACTION-IF-NOT-FOUND]])

the macro PKG_CHECK_MODULES can be used in configure.ac to check whether modules exist. a typical usage would be:

PKG_CHECK_MODULES([MYSTUFF], [gtk+-2.0 >= 1.3.5 libxml = 1.8.4])

this would result in MYSTUFF_LIBS and MYSTUFF_CFLAGS substitution variables, set to the libs and cflags for the given module list. if a module is missing or has the wrong version, by default configure will abort with a message. to swap out the default action, specify an ACTION-IF-NOT-FOUND. PKG_CHECK_MODULES will not print any error messages if you specify your own ACTION-IF-NOT-FOUND β€” however, it will set the variable MYSTUFF_PKG_ERRORS, which you can use to display what went wrong 🫢

heads up: if there's a possibility the first call to PKG_CHECK_MODULES might not happen, you should be sure to include an explicit call to PKG_PROG_PKG_CONFIG in your configure.ac.

also note: repeated usage of VARIABLE-PREFIX is not recommended. after the first successful usage, subsequent calls with the same VARIABLE-PREFIX will simply use the _LIBS and _CFLAGS variables set from the previous usage β€” without calling pkg-config again. lazy but make it efficient πŸ’…

PKG_PROG_PKG_CONFIG([MIN-VERSION])

defines the PKG_CONFIG variable to the best pkg-config available. useful if you need pkg-config but don't wanna use PKG_CHECK_MODULES ✨

PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])

checks whether a particular set of modules exists. similar to PKG_CHECK_MODULES(), but it does not set variables or print errors. and similar to PKG_CHECK_MODULES, make sure the first instance of this or PKG_CHECK_MODULES is called β€” or make sure to call PKG_CHECK_EXISTS manually 😀

PKG_INSTALLDIR(DIRECTORY)

substitutes the variable pkgconfigdir as the location where a module should install pkg-config .pc files. by default the directory is $libdir/pkgconfig, but that default can be changed by passing DIRECTORY. the user can override it through the --with-pkgconfigdir parameter πŸŽ€

PKG_NOARCH_INSTALLDIR(DIRECTORY)

substitutes the variable noarch_pkgconfigdir as the location where a module should install arch-independent pkg-config .pc files. by default the directory is $datadir/pkgconfig, but the default can be changed by passing DIRECTORY. the user can override it through the --with-noarch-pkgconfigdir parameter 🌸

PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])

retrieves the value of the pkg-config variable CONFIG-VARIABLE from MODULE and stores it in VARIABLE. note that repeated usage of VARIABLE is not recommended, since the check will be skipped if the variable is already set πŸ‘€


πŸ“„ METADATA FILE SYNTAX πŸ“„

to add a library to the set of packages pkg-config knows about, simply install a .pc file. you should install this file to libdir/pkgconfig πŸ’–

here's an example file, look at her go:

# This is a comment
prefix=/home/hp/unst   # this defines a variable
exec_prefix=${prefix}  # defining another variable in terms of the first
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: GObject                            # human-readable name
Description: Object/type system for GLib # human-readable description
Version: 1.3.1
URL: http://www.gtk.org
Requires: glib-2.0 = 1.3.1
Conflicts: foobar <= 4.5
Libs: -L${libdir} -lgobject-1.3
Libs.private: -lm
Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib/include

you'd normally generate the file using configure, so that the prefix, etc. are set to the proper values. the GNU Autoconf manual recommends generating files like .pc files at build time rather than configure time β€” so when you build the .pc file is a matter of taste and preference. you do you, bestie πŸ“

files have two kinds of line: keyword lines start with a keyword plus a colon, and variable definitions start with an alphanumeric string plus an equals sign. keywords are defined in advance and have special meaning to pkg-config; variables don't β€” you can have any variables that you wish (however, users may expect to retrieve the usual directory name variables) ✨

note that variable references are written ${foo}; you can escape a literal ${ as $${.

Name:

this field should be a human-readable name for the package. note that it is not the name passed as an argument to pkg-config β€” don't get it twisted 😭

Description:

this should be a brief description of the package.

URL:

an URL where people can get more information about and download the package.

Version:

this should be the most-specific-possible package version string.

Requires:

this is a comma-separated list of packages that are required by your package. flags from dependent packages will be merged in to the flags reported for your package. optionally, you can specify the version of the required package (using the operators =, <, >, >=, <=); specifying a version allows pkg-config to perform extra sanity checks. you may only mention the same package one time on the Requires: line. if the version of a package is unspecified, any version will be used with no checking. periodt.

Requires.private:

a list of packages required by this package. the difference from Requires is that the packages listed under Requires.private are not taken into account when a flag list is computed for a dynamically linked executable (i.e., when --static was not specified). in the situation where each .pc file corresponds to a library, Requires.private shall be used exclusively to specify the dependencies between the libraries πŸ’…

Conflicts:

this optional line allows pkg-config to perform additional sanity checks, primarily to detect broken user installations 😬 the syntax is the same as Requires: except that you can list the same package more than once here β€” for example "foobar = 1.2.3, foobar = 1.2.5, foobar >= 1.3", if you have reason to do so. if a version isn't specified, then your package conflicts with all versions of the mentioned package. and if a user tries to use your package and a conflicting package at the same time, then pkg-config will complain. loudly. πŸ—£οΈ

Libs:

this line should give the link flags specific to your package. don't add any flags for required packages β€” pkg-config will add those automatically, she's got it πŸ”₯

Libs.private:

this line should list any private libraries in use. private libraries are libraries which are not exposed through your library, but are needed in the case of static linking. this differs from Requires.private in that it references libraries that do not have package files installed πŸ™ˆ

Cflags:

this line should list the compile flags specific to your package. don't add any flags for required packages β€” pkg-config will add those automatically ✨


πŸ‘‘ AUTHOR πŸ‘‘

pkg-config was written by James Henstridge, rewritten by Martijn van Beers, and rewritten again by Havoc Pennington. Tim Janik, Owen Taylor, and Raja Harinath submitted suggestions and some code. gnome-config was written by Miguel de Icaza, Raja Harinath and various hackers in the GNOME team. it was inspired by Owen Taylor's gtk-config program. literal legends, all of them πŸ«ΆπŸ’–


πŸ’€ BUGS πŸ’€

pkg-config does not handle mixing of parameters with and without = well. stick with one. fr fr, pick a lane 😀

bugs can be reported at http://bugs.freedesktop.org/ under the pkg-config component.


and that's the whole page, bestie!! πŸŽ‰ you just read all of pkg-config(1) β€” go forth and .pc with confidence, you absolute icon πŸŒˆπŸ’œ

stay slay, stay linked, and may your flags always resolve β€” mwah πŸ’‹

πŸ“¦ where this page comes from