TExcelTAD
Home › Meson, Autotools & xmake › dependency(), pkg-config & declare_dependency

dependency(), pkg-config & declare_dependency

In Meson, everything a target needs from outside (include paths, compiler flags, libraries to link) travels as one dependency object. You get one from dependency('name'), which asks the system, or make one yourself with declare_dependency(). Targets take a list of them with dependencies:.

How dependency() looks things up

dependency('foo') tries, by default, in this order:

  1. pkg-config: is there a foo.pc on the pkg-config search path?
  2. CMake: is there a fooConfig.cmake / FindFoo.cmake package? (Meson runs CMake in the background for that)
  3. Built-in system lookups for a few special dependencies (threads, openmp, boost, dl, zlib on some platforms…).
  4. A fallback subproject: one named in fallback:, one whose wrap file provides foo, or simply a subprojects/foo/ directory with the same name (see Subprojects & Wrap Files).

The method: keyword pins one (method: 'pkg-config'). pkg-config is the native language of Unix libraries, and the one this lesson uses.

A system dependency

The course image has the development files of libxcrypt (the password-hashing library behind crypt()), which ship a libxcrypt.pc:

bash · try it yourself
mkdir -p pw && cd pw
cat > meson.build <<'M'
project('pw', 'c')
crypt = dependency('libxcrypt', version: '>=4.0')
message('libxcrypt ' + crypt.version() + ', libdir ' + crypt.get_variable(pkgconfig: 'libdir'))
executable('pw', 'main.c', dependencies: crypt)
M
cat > main.c <<'SRC'
#include <crypt.h>
#include <stdio.h>
int main(void) {
    const char *h = crypt("secret", "$1$saltsalt
quot;); printf("%s\n", h ? h : "(null)"); return 0; } SRC meson setup build | grep -E 'pkg-config|dependency|Message' meson compile -C build -j 1 > /dev/null && ./build/pw pkg-config --cflags --libs libxcrypt
Found pkg-config: YES (/usr/bin/pkg-config) 1.8.1
Run-time dependency libxcrypt found: YES 4.4.36
Message: libxcrypt 4.4.36, libdir /usr/lib/x86_64-linux-gnu
$1$saltsalt$9xy1btjgzLYfb7hivXtC//
-lcrypt

version: takes the usual comparison strings ('>=4.0', '<5', or a list of several). The dependency object carries what pkg-config --cflags --libs would print, and get_variable(pkgconfig: …) reads any other variable from the .pc file.

When a dependency is missing

bash · try it yourself
mkdir -p z && cd z
cat > meson.build <<'M'
project('z', 'c')
zlib = dependency('zlib', method: 'pkg-config')
M
meson setup build 2>&1 | tail -4

meson.build:2:7: ERROR: Dependency "zlib" not found (tried pkg-config)

A full log can be found at /home/learner/z/build/meson-logs/meson-log.txt

The fix is to install the library's development package (zlib1g-dev on Debian/Ubuntu, zlib-devel on Fedora), or to point pkg-config at your own build of it. To make the dependency optional, use required: false and check found(), or better, a feature option (next-but-one lesson):

meson
zlib = dependency('zlib', required: false)
if zlib.found()
  add_project_arguments('-DHAVE_ZLIB', language: 'c')
endif

Your own libraries through pkg-config

Libraries you build and install yourself (or that a CI job unpacks somewhere) work the same way: put their lib/pkgconfig directory on the search path. Here a tiny library is compiled by hand and installed into ~/deps with a .pc file, then found through Meson's pkg_config_path option. The PKG_CONFIG_PATH environment variable works too, but the option is stored in the build directory, so a later automatic reconfigure (from another shell, an IDE or CI step) still finds the library.

bash · try it yourself
# 1. a hand-made "installed" library: ~/deps/{include,lib,lib/pkgconfig}
mkdir -p ~/deps/include ~/deps/lib/pkgconfig src
echo 'int ring_size(void);' > ~/deps/include/ring.h
echo 'int ring_size(void) { return 64; }' > src/ring.c
gcc -c src/ring.c -o src/ring.o && ar rcs ~/deps/lib/libring.a src/ring.o
cat > ~/deps/lib/pkgconfig/ring.pc <<'PC'
prefix=/home/learner/deps
includedir=${prefix}/include
libdir=${prefix}/lib

Name: ring
Description: A ring buffer
Version: 2.1.0
Cflags: -I${includedir}
Libs: -L${libdir} -lring
PC
# 2. a Meson project that uses it
mkdir -p app && cd app
cat > meson.build <<'M'
project('app', 'c')
ring = dependency('ring', version: '>=2')
executable('app', 'main.c', dependencies: ring)
M
echo '#include <stdio.h>
#include <ring.h>
int main(void) { printf("ring size %d\n", ring_size()); return 0; }' > main.c
echo "--- without a search path"
meson setup build 2>&1 | grep -E 'dependency ring|ERROR'
echo "--- with -Dpkg_config_path"
meson setup build --wipe -Dpkg_config_path=$HOME/deps/lib/pkgconfig | grep 'dependency ring'
meson compile -C build -j 1 > /dev/null && ./build/app
--- without a search path
Run-time dependency ring found: NO  (tried pkg-config and cmake)
meson.build:2:7: ERROR: Dependency "ring" not found (tried pkg-config and cmake)
--- with -Dpkg_config_path
Run-time dependency ring found: YES 2.1.0
ring size 64

--wipe throws away a build directory's configuration and sets it up again with the new options (here the first, failed setup had left a half-made build/).

declare_dependency(): package your own targets

Inside one project, you could pass link_with: and include_directories: to every program that uses a library. declare_dependency() bundles them into one object, exactly like a CMake target with PUBLIC usage requirements:

bash · try it yourself
mkdir -p proj/lib && cd proj
cat > meson.build <<'M'
project('proj', 'c')
subdir('lib')
executable('app', 'main.c', dependencies: shapes_dep)
M
cat > lib/meson.build <<'M'
shapes_lib = static_library('shapes', 'shapes.c', c_args: ['-DSHAPES_BUILDING'])
shapes_dep = declare_dependency(
  link_with: shapes_lib,
  include_directories: include_directories('.'),
  compile_args: ['-DSHAPES_UNITS="cm"'])      # consumers get this, the library does not
M
echo 'double square_area(double side);' > lib/shapes.h
echo 'double square_area(double s) { return s * s; }' > lib/shapes.c
cat > main.c <<'SRC'
#include <stdio.h>
#include "shapes.h"
int main(void) { printf("area %.1f %s^2\n", square_area(3), SHAPES_UNITS); return 0; }
SRC
meson setup build > /dev/null
meson compile -C build -j 1 > /dev/null && ./build/app
area 9.0 cm^2

The real payoff is consistency: a dependency object from declare_dependency() looks exactly like one from dependency(). A project can therefore use a library either from the system (pkg-config) or built from source as a subproject, with the same dependencies: foo_dep line. The subprojects lesson builds on this.

CMake Meson
find_package(ZLIB REQUIRED) zlib = dependency('zlib')
target_link_libraries(app PRIVATE ZLIB::ZLIB) executable('app', …, dependencies: zlib)
target_include_directories(lib PUBLIC include) + target_link_libraries(app PRIVATE lib) lib_dep = declare_dependency(link_with: lib, include_directories: …) + dependencies: lib_dep
CMAKE_PREFIX_PATH -Dpkg_config_path=…, -Dcmake_prefix_path=…

Test yourself

What does dependency('foo') try first on Linux by default?

  1. CMake config packages
  2. pkg-config (foo.pc)
  3. A wrap file
  4. /usr/lib/libfoo.so directly

Why is declare_dependency() useful even inside a single project?

  1. It makes the build faster
  2. It bundles link targets, include paths and flags into one dependency object that consumers use like any dependency() result
  3. It installs the library
  4. It creates a pkg-config file

Meson can't find your library in ~/deps. Which option stores the search path in the build directory?

  1. -Dlibdir=~/deps/lib
  2. -Dpkg_config_path=$HOME/deps/lib/pkgconfig
  3. --prefix=~/deps
  4. -Dc_args=-L~/deps/lib