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 [OPTIONS] [LIBRARIES...]
PARAMETERS
--cflags
Output pre-processor and compiler flags for the specified libraries.
--libs
Output linker flags for the specified libraries.
--modversion
Output the version string of the specified module.
--list-all
List all modules that pkg-config knows about.
--exists
Check if the specified modules exist; returns 0 if all exist, non-zero otherwise.
--atleast-version=VERSION
Check if the module's version is at least the specified VERSION.
--variable=NAME
Output the value of a specific variable defined within the module's .pc file.
--static
Output flags suitable for static linking, often including full dependency closure.
--uninstalled
Look for uninstalled modules, useful during library development.
--with-path=PATH
Add PATH to the search path for .pc files, overriding PKG_CONFIG_PATH for this command.
--debug
Enable verbose debug output to assist with troubleshooting.
--silence-errors
Suppress all error messages, useful when only the exit status is needed.
DESCRIPTION
pkg-config is a command-line utility that simplifies the process of compiling and linking applications against installed libraries. It queries metadata files, typically with a .pc extension, which libraries install in standard locations like /usr/lib/pkgconfig. These .pc files contain crucial information such as header file locations (-I flags), library paths (-L flags), library names (-l flags), version numbers, and dependencies. By automating the retrieval of these compilation and linking flags, pkg-config eliminates the need for developers to hardcode paths or options. This makes build systems more robust, portable, and easier to manage across different operating systems or environments, significantly reducing potential errors when library versions or locations change. It is widely adopted in open-source projects, serving as a critical component in many modern build workflows.
CAVEATS
pkg-config's functionality is dependent on libraries providing correct and complete .pc files, which is not universally guaranteed. For non-standard installations or cross-compilation environments, proper configuration of environment variables such as PKG_CONFIG_PATH and PKG_CONFIG_SYSROOT_DIR is crucial. While effective for direct dependencies, complex or deeply nested transitive dependencies might occasionally require manual intervention or deeper understanding of the specific library's build requirements.
<B>PKG_CONFIG_PATH ENVIRONMENT VARIABLE</B>
This crucial environment variable specifies a colon-separated list of directories where pkg-config searches for .pc files. It is essential for locating libraries installed in non-standard or custom prefix locations (e.g., /usr/local/lib/pkgconfig, ~/my_libs/lib/pkgconfig), ensuring that pkg-config can find all necessary module definitions.
<B>.PC FILES</B>
These plain-text metadata files are fundamental to pkg-config's operation. Each .pc file provides details about a specific library, including its version, installation prefix, required compiler flags (Cflags), linker flags (Libs), and direct dependencies (Requires, Conflicts). Libraries typically install their corresponding .pc file into a pkgconfig subdirectory within their libdir (e.g., /usr/lib/x86_64-linux-gnu/pkgconfig/gtk+-3.0.pc), making their build information discoverable.
<B>CROSS-COMPILATION SUPPORT</B>
pkg-config includes robust support for cross-compilation. This is achieved by setting environment variables such as PKG_CONFIG_SYSROOT_DIR, which points to the sysroot of the target architecture, and PKG_CONFIG_LIBDIR, which specifies the path to the pkgconfig directory within that sysroot. This setup allows build systems to accurately query libraries intended for a different architecture than the host system, ensuring correct compilation and linking for diverse targets.
HISTORY
pkg-config originated within the GNOME project to standardize and simplify the process of finding and using library dependencies, replacing less flexible predecessors like gnome-config and kde-config. Its core innovation was the introduction of standardized .pc metadata files. This design offered a consistent and portable method for libraries to expose their build-time information. Due to its simplicity, efficiency, and the portability benefits it offered, pkg-config quickly became a de facto standard in Linux and Unix-like build environments, extending its usage far beyond its initial GNOME-centric scope to become a widely adopted tool across various open-source projects.