Executables, Libraries & subdir()
Meson has one function per kind of target: executable(), static_library(), shared_library(), library() (static or shared, decided by the default_library option), both_libraries(), shared_module() (a plugin loaded with dlopen), plus custom_target() and run_target(). Every one returns a target object that other targets link to.
A library and a program
mkdir -p calc && cd calc
cat > meson.build <<'M'
project('calc', 'c', version: '1.4.0')
inc = include_directories('include')
calc = library('calc', 'src/add.c', 'src/mul.c',
include_directories: inc,
version: meson.project_version(), # libcalc.so.1.4.0
soversion: '1') # soname libcalc.so.1
executable('calc-cli', 'src/main.c',
include_directories: inc,
link_with: calc)
M
mkdir -p include src
cat > include/calc.h <<'H'
int calc_add(int a, int b);
int calc_mul(int a, int b);
H
echo '#include "calc.h"
int calc_add(int a, int b) { return a + b; }' > src/add.c
echo '#include "calc.h"
int calc_mul(int a, int b) { return a * b; }' > src/mul.c
cat > src/main.c <<'SRC'
#include <stdio.h>
#include "calc.h"
int main(void) { printf("2+3=%d 2*3=%d\n", calc_add(2, 3), calc_mul(2, 3)); return 0; }
SRC
meson setup build > /dev/null
meson compile -C build -j 1
./build/calc-cli
ls build | grep libcalc
readelf -d build/libcalc.so.1.4.0 | grep SONAMEINFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/local/bin/ninja -C /home/learner/calc/build -j 1
ninja: Entering directory `/home/learner/calc/build'
[1/6] Compiling C object libcalc.so.1.4.0.p/src_add.c.o
[2/6] Compiling C object libcalc.so.1.4.0.p/src_mul.c.o
[3/6] Linking target libcalc.so.1.4.0
[4/6] Generating symbol file libcalc.so.1.4.0.p/libcalc.so.1.4.0.symbols
[5/6] Compiling C object calc-cli.p/src_main.c.o
[6/6] Linking target calc-cli
2+3=5 2*3=6
libcalc.so
libcalc.so.1
libcalc.so.1.4.0
libcalc.so.1.4.0.p
0x000000000000000e (SONAME) Library soname: [libcalc.so.1]
Meson names object directories after their target (libcalc.so.1.4.0.p/; the src_add.c.o object name keeps the source path, so two util.c files in different directories don't collide), creates the soname symlinks itself, and gives the program an RPATH into the build directory so ./build/calc-cli runs without installing (the RPATH is removed at install time).
Step 4, Generating symbol file, is a Ninja trick Meson uses for every shared library: it writes the library's list of exported symbols into a .symbols file and only updates that file when the list changes. Programs depend on the symbol file, not on the .so itself, so editing the body of calc_add recompiles and relinks the library but does not relink calc-cli.
Static or shared? Let the user decide
library() follows the built-in option default_library (shared, static or both). That is the Meson equivalent of CMake's BUILD_SHARED_LIBS, and it is the recommended function for libraries: packagers pick, not you.
mkdir -p calc/src && cd calc
cat > meson.build <<'M'
project('calc', 'c')
calc = library('calc', 'src/add.c')
executable('calc-cli', 'src/main.c', link_with: calc)
M
echo 'int calc_add(int a, int b) { return a + b; }' > src/add.c
echo 'int calc_add(int, int); int main(void) { return calc_add(1, 1) - 2; }' > src/main.c
meson setup build-static -Ddefault_library=static > /dev/null
meson compile -C build-static -j 1 | grep '^\['
meson setup build-both -Ddefault_library=both > /dev/null
meson compile -C build-both -j 1 | grep '^\['[1/4] Compiling C object libcalc.a.p/src_add.c.o
[2/4] Linking static target libcalc.a
[3/4] Compiling C object calc-cli.p/src_main.c.o
[4/4] Linking target calc-cli
[1/6] Compiling C object libcalc.so.p/src_add.c.o
[2/6] Linking target libcalc.so
[3/6] Generating symbol file libcalc.so.p/libcalc.so.symbols
[4/6] Compiling C object calc-cli.p/src_main.c.o
[5/6] Linking static target libcalc.a
[6/6] Linking target calc-cli
With both, the program links against the shared version (option default_both_libraries changes that).
subdir(): one meson.build per directory
Big projects split their description with subdir('dir'), which runs dir/meson.build in the same scope: variables defined in the subdirectory are visible afterwards in the parent, in the order the calls appear. That is very different from CMake's add_subdirectory, where each directory has its own variable scope.
mkdir -p proj/lib proj/app && cd proj
cat > meson.build <<'M'
project('proj', 'c')
subdir('lib') # defines greet_lib and greet_inc
subdir('app') # uses them
M
cat > lib/meson.build <<'M'
greet_inc = include_directories('.')
greet_lib = static_library('greet', 'greet.c')
M
cat > app/meson.build <<'M'
executable('app', 'main.c', link_with: greet_lib, include_directories: greet_inc)
M
echo 'const char *greet(void);' > lib/greet.h
echo 'const char *greet(void) { return "hi from lib/"; }' > lib/greet.c
echo '#include <stdio.h>
#include "greet.h"
int main(void) { puts(greet()); return 0; }' > app/main.c
meson setup build > /dev/null
meson compile -C build -j 1 | grep '^\['
./build/app/app[1/4] Compiling C object lib/libgreet.a.p/greet.c.o
[2/4] Linking static target lib/libgreet.a
[3/4] Compiling C object app/app.p/main.c.o
[4/4] Linking target app/app
hi from lib/
The build directory mirrors the source tree (build/lib/libgreet.a, build/app/app). Because the order of subdir() calls matters, put libraries before the programs that use them.
Files from another directory: files()
A plain string like 'greet.c' is relative to the meson.build that uses it. To build a list in one directory and use it in another, wrap it in files(), which resolves the path once, where it is written:
# lib/meson.build
greet_sources = files('greet.c', 'util.c')
# meson.build (top level), after subdir('lib')
static_library('greet', greet_sources)Test yourself
Which function lets the user choose static or shared with -Ddefault_library=...?
shared_library()static_library()library()shared_module()
A variable is set in lib/meson.build. Can the top-level meson.build use it after subdir('lib')?
- No, each directory has its own scope
- Yes,
subdir()runs in the same scope - Only if it is exported with
set_variable - Only for target objects
What do version: '1.4.0' and soversion: '1' produce on Linux?
libcalc.so.1.4.0with sonamelibcalc.so.1libcalc-1.4.0.solibcalc.so.1only- A static library