$linuxjunkies
>

pkg-config(1)

pkg-config is a helper tool used when compiling applications and libraries to find installed packages, obtain their compiler and linker flags, and check package versions.

UbuntuDebianFedoraArch

Synopsis

pkg-config [OPTION]... [LIBRARY]...

Description

pkg-config is a utility that retrieves information about installed libraries from .pc metadata files. It returns compiler flags, linker flags, version numbers, and other package metadata, making it essential for build systems to locate and link against dependencies correctly.

The tool reads .pc files from standard locations like /usr/lib/pkgconfig and /usr/share/pkgconfig, and can be configured with environment variables to search custom directories. It's widely used in autoconf, CMake, and Meson build systems.

Common options

FlagWhat it does
--cflagsoutput all preprocessor and compiler flags required to compile
--libsoutput all linker flags and libraries needed to link
--modversionoutput the version of the specified package
--existscheck if package(s) exist; exit with status code only, no output
--atleast-version=VERSIONexit successfully if package version is >= specified version
--list-alllist all .pc files found in the search path
--variable=NAMEoutput the value of a variable defined in the .pc file
--define-variable=NAME=VALUEdefine a variable for use in .pc file expansion
--cflags-only-Ioutput only -I include directory flags
--libs-only-loutput only -l library name flags
--libs-only-Loutput only -L library directory flags

Examples

get compiler and linker flags for GTK+ 3.0 libraries

pkg-config --cflags --libs gtk+-3.0

check the installed version of OpenSSL

pkg-config --modversion openssl

test whether zlib development package is installed

pkg-config --exists zlib && echo 'zlib installed' || echo 'zlib not found'

compile a program using pkg-config to inject libcurl flags automatically

gcc -o myapp myapp.c $(pkg-config --cflags --libs libcurl)

list all installed packages matching 'gnome'

pkg-config --list-all | grep gnome

output the include directory path for glib-2.0

pkg-config --variable=includedir glib-2.0

Related commands