pkg-config
Retrieve compiler and linker flags for libraries
TLDR
Get the list of libraries and their dependencies
Get the list of libraries, their dependencies, and proper cflags for gcc
Compile your code with libgtk-3, libwebkit2gtk-4.0 and all their dependencies
SYNOPSIS
pkg-config [--modversion] [--version] [--libs] [--cflags] [--exists] [--atleast-version=VERSION] [--exact-version=VERSION] [--max-version=VERSION] [MODULE...]
PARAMETERS
--modversion
Prints the version of the specified module.
--version
Prints the version of pkg-config itself.
--libs
Prints the linker flags required to link with the specified module(s).
--cflags
Prints the compiler flags required to compile against the specified module(s).
--exists
Returns 0 if the specified module(s) exist and the specified version constraints are satisfied, otherwise returns 1.
--atleast-version=VERSION
Checks if the specified module(s) have at least the given version.
--exact-version=VERSION
Checks if the specified module(s) have exactly the given version.
--max-version=VERSION
Checks if the specified module(s) have no later version than the given version.
MODULE...
The name(s) of the module(s) to query.
DESCRIPTION
pkg-config is a command-line tool that retrieves information about installed libraries. It primarily helps resolve compile and link flags needed to build software that depends on these libraries. It simplifies the process of specifying include directories and library paths for compilers and linkers. pkg-config works by reading special metadata files (.pc
files) provided by libraries. These files contain information like the library's name, version, include directories, library paths, and dependencies on other libraries. By querying these metadata files, pkg-config eliminates the need to manually determine the correct compiler and linker flags, ensuring consistency and reducing errors in build processes. The tool is invaluable for software developers and build systems, especially when dealing with complex projects that rely on multiple libraries. It promotes portability and simplifies cross-platform builds by abstracting away platform-specific details.
CAVEATS
pkg-config relies on correctly configured .pc
files. If these files are missing or incorrectly configured, pkg-config will not function properly. The PKG_CONFIG_PATH
environment variable must be set correctly to point to the directories containing the .pc
files.
EXAMPLE USAGE
To get the compiler flags for the libpng library:
pkg-config --cflags libpng
To get the linker flags for the libpng library:pkg-config --libs libpng
To check if libpng is installed and has at least version 1.2.0:pkg-config --exists --atleast-version=1.2.0 libpng
PKG_CONFIG_PATH
The PKG_CONFIG_PATH
environment variable specifies the directories where pkg-config searches for .pc
files. Multiple directories can be separated by colons (:). It is crucial to set this variable correctly to ensure pkg-config can find the metadata files for your installed libraries.
Example: export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/opt/my_library/lib/pkgconfig
HISTORY
pkg-config emerged as a solution to the complexities of managing dependencies in software development. Prior to its widespread adoption, developers often had to manually specify include directories and library paths, which was prone to errors and inconsistencies. pkg-config standardized this process by providing a consistent way to query information about installed libraries. It became increasingly popular with the rise of open-source software and the proliferation of libraries, helping to simplify build processes and improve portability.