pkg-config: Usage, Options & Common CI Errors
pkg-config reports the -I and -l flags a library needs from its .pc file.
pkg-config is the glue that lets builds discover library flags without hardcoding paths. The defining CI error - "Package X was not found in the pkg-config search path" - is almost always a missing -dev package or an unset PKG_CONFIG_PATH.
What it does
pkg-config reads .pc metadata files installed alongside libraries and prints the compiler and linker flags needed to use them. Build systems call it (often via $(pkg-config --cflags --libs foo)) so they do not hardcode include and library paths.
Common usage
pkg-config --cflags --libs openssl
gcc main.c \$(pkg-config --cflags --libs glib-2.0) -o app
pkg-config --exists libfoo && echo present
pkg-config --modversion zlib
export PKG_CONFIG_PATH=/opt/lib/pkgconfig:\$PKG_CONFIG_PATHOptions
| Flag | What it does |
|---|---|
| --cflags | Print include flags (-I, -D) |
| --libs | Print link flags (-L, -l) |
| --exists | Exit 0 if the package is found |
| --modversion | Print the installed version |
| PKG_CONFIG_PATH | Extra dirs to search for .pc files |
Common errors in CI
"Package foo was not found in the pkg-config search path. Perhaps you should add the directory containing ‘foo.pc’ to the PKG_CONFIG_PATH" - install the library’s -dev/-devel package (the .pc ships there) or export PKG_CONFIG_PATH to its location. For cross builds the host .pc files are wrong; use a target sysroot and PKG_CONFIG_LIBDIR. "pkg-config: command not found" on slim images - install pkg-config (or pkgconf). A bad --modversion comparison can also abort configure scripts.