Skip to content
Latchkey

pkg-config --cflags --libs: Compiler and Linker Flags

pkg-config prints the exact -I and -l flags a project needs to compile and link against an installed library.

pkg-config reads .pc metadata files that libraries install and turns them into compiler and linker flags, so build scripts do not hardcode paths. It is the dependency-discovery layer beneath CMake and Meson on Unix.

What it does

pkg-config --cflags <pkg> prints the include flags (-I, -D) and --libs prints the link flags (-L, -l) recorded in the library's .pc file. --static adds the private dependencies needed for static linking. The output is meant to be embedded directly in a compile or link command.

Common usage

Terminal
pkg-config --cflags libcurl
pkg-config --libs libcurl
gcc $(pkg-config --cflags --libs libcurl) app.c -o app
pkg-config --libs --static zlib   # include private deps for static link

Options

FlagWhat it does
--cflags <pkg>Print include/preprocessor flags
--libs <pkg>Print library/link flags
--staticInclude private deps for static linking
--cflags-only-I / --libs-only-LPrint only include or only library dirs
--modversion <pkg>Print the installed version

In CI

Install the development packages (the -dev/-devel variants), not just the runtime libraries, because the .pc files ship with the dev package. When a build fails to find a library that is "installed", it is almost always the dev package or its .pc file that is missing on the runner.

Common errors in CI

"Package libcurl was not found in the pkg-config search path" means the .pc file is absent (missing -dev package) or not on the search path; install the dev package or set PKG_CONFIG_PATH. "Perhaps you should add the directory containing 'libcurl.pc'" is the same cause. Empty --libs output usually means a malformed or partial .pc file.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →