Skip to content
Latchkey

pkg-config --exists: Probe for a Dependency

pkg-config --exists returns an exit code you can branch on to test whether a library (and version) is available.

Before compiling, a CI script can ask pkg-config whether a dependency is present and recent enough, failing early with a clear message instead of deep in a compiler error.

What it does

pkg-config --exists <pkg> exits 0 if the package is found and non-zero otherwise, printing nothing, which makes it ideal for shell conditionals. --atleast-version, --exact-version, and --max-version add version constraints to the same exit-code test. --print-errors makes it explain a failure.

Common usage

Terminal
pkg-config --exists libssl || { echo "libssl-dev missing"; exit 1; }
pkg-config --atleast-version=1.1.1 openssl \
  || { echo "openssl too old"; exit 1; }
pkg-config --exists --print-errors "glib-2.0 >= 2.56"

Options

FlagWhat it does
--exists <pkg>Exit 0 if the package is found
--atleast-version=<v>Require at least version v
--exact-version=<v> / --max-version=<v>Require an exact or maximum version
--print-errorsPrint why the check failed
--modversionPrint the found version

In CI

Put a few --exists / --atleast-version checks at the top of the build script so a missing dev package fails fast with a human message naming the package, instead of an obscure linker error twenty minutes later. Combine the package and version in one quoted argument: "glib-2.0 >= 2.56".

Common errors in CI

--exists succeeding but the build still failing to link means the .pc exists but is broken or the wrong architecture; check --cflags/--libs output. A version check that always fails on a present library is usually a quoting issue (quote "pkg >= x"). --print-errors will name the exact unmet requirement when the bare exit code is not enough.

Related guides

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