Meson Tutorial for Beginners Pt. 4
Next, we are going to use external library in our project. For the demonstration purpose, we will use zlib, a data compression library that is available on many platforms. To install it, run one of the suitable command for your system.
sudo apt install zlib1g-dev
# or
sudo dnf install zlib-devel
# or
brew install zlibLet’s use zlib in our code to print the version of library using zlibVersion() function as follow in main.c.
#include "greetings.h"
#include <stdio.h>
#include <zlib.h>
int main(void) {
greet();
printf("zlib version: %s\n", zlibVersion());
return 0;
}Let’s compile the project.
$ meson compile -C builddir
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /usr/bin/ninja -C /hello_meson/builddir
ninja: Entering directory `/hello_meson/builddir'
[2/2] Linking target main
FAILED: main
cc -o main main.p/main.c.o main.p/greetings.c.o -Wl,--as-needed -Wl,--no-undefined
/usr/bin/ld: main.p/main.c.o: in function `main':
/hello_meson/builddir/../main.c:7:(.text+0xa): undefined reference to `zlibVersion'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.Compilation failed. Because, compiler doesn’t know where to find zlib.h or how to link zlibVersion() function.
Let’s update the meson.build file. We are going to use dependency() function to find the zlib and then pass to executable() function.
project(
'hello-meson',
'c',
default_options: [
'warning_level=2',
'c_std=c17'
]
)
sources = [
'main.c',
'greetings.c'
]
executable(
'main',
sources,
dependencies :dependency('zlib')
)Now, meson can look for the zlib. But, run following command first otherwise you will get an error.
meson setup --reconfigure builddir
The Meson build system
Version: 1.7.0
Source dir: /hello_meson
Build dir: /hello_meson/builddir
Build type: native build
Project name: hello-meson
Project version: undefined
C compiler for the host machine: ccache cc (gcc 14.2.0 "cc (Debian 14.2.0-19) 14.2.0")
C linker for the host machine: cc ld.bfd 2.44
Host machine cpu family: x86_64
Host machine cpu: x86_64
Found pkg-config: YES (/usr/bin/pkg-config) 1.8.1
Run-time dependency zlib found: YES 1.3.1
Build targets in project: 1
Found ninja-1.12.1 at /usr/bin/ninja
Cleaning... 0 files.As we have changed the logic to look for the new library, it is good practice to reconfigure first.
meson compile -C builddir
./builddir/main
hello, world
zlib version: 1.3.1Instead of directly writing the dependency in executable() function, we an define a variable as
project(
'hello-meson',
'c',
default_options: [
'warning_level=2',
'c_std=c17'
]
)
sources = [
'main.c',
'greetings.c'
]
zlib = dependency('zlib')
executable(
'main',
sources,
dependencies : zlib
)Let’s run the project again and confirm the output. It suppose to be same.
meson compile -C builddir
./builddir/main
hello, world
zlib version: 1.3.1If needed, we can define a version of library as,
zlib = dependency('zlib', version: '>=1.2.0')Finally, if we have multiple libraries as dependency e.g. zlib and libfoo then it should be written as list for the dependencies.
zlib = dependecy('zlib')
foo = dependecny('foo')
deps = [
zlib,
foo
]
executable(
'main',
sources,
dependencies: deps
)Don’t forget to reconfigure the project when new library being added!
Yay!