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:
- pkg-config: is there a
foo.pcon the pkg-config search path? - CMake: is there a
fooConfig.cmake/FindFoo.cmakepackage? (Meson runs CMake in the background for that) - Built-in system lookups for a few special dependencies (
threads,openmp,boost,dl,zlibon some platforms…). - A fallback subproject: one named in
fallback:, one whose wrap file providesfoo, or simply asubprojects/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:
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$saltsaltquot;);
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 libxcryptFound 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
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):
zlib = dependency('zlib', required: false)
if zlib.found()
add_project_arguments('-DHAVE_ZLIB', language: 'c')
endifYour 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.
# 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:
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/apparea 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?
- CMake config packages
- pkg-config (
foo.pc) - A wrap file
/usr/lib/libfoo.sodirectly
Why is declare_dependency() useful even inside a single project?
- It makes the build faster
- It bundles link targets, include paths and flags into one dependency object that consumers use like any
dependency()result - It installs the library
- It creates a pkg-config file
Meson can't find your library in ~/deps. Which option stores the search path in the build directory?
-Dlibdir=~/deps/lib-Dpkg_config_path=$HOME/deps/lib/pkgconfig--prefix=~/deps-Dc_args=-L~/deps/lib