Meson Tutorial for Beginners Pt. 3
Next, we are interested to pass some options to compilation process such as -Wall and -Wextra to show the possible code warnings. Apart from this, we are also interested in to set the C standard such as std17.
We can use add_project_arguments() function as,
project('hello-meson', 'c')
sources = [
'main.c',
'greetings.c'
]
add_project_arguments('-Wall', '-Wextra',language: 'c')
executable('main', sources)This will add these compilation options directly to the compile command. If you want to see if these flags are passed to compilation or not, you can use -v or --verbose to see the exact command meson will run. For example, in my case while running the following command,
meson compile -v -C builddirI get the following output.
# ...
Host machine cpu: x86_64
../meson.build:9: WARNING: Consider using the built-in warning_level option instead of using "-Wall".
../meson.build:9: WARNING: Consider using the built-in warning_level option instead of using "-Wextra".
Build targets in project: 1
Found ninja-1.12.1 at /usr/bin/ninja
Cleaning... 0 files.
[1/4] ccache cc -Imain.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -Wall -Wextra -MD -MQ main.p/main.c.o -MF main.p/main.c.o.d -o main.p/main.c.o -c ../main.c
[2/4] ccache cc -Imain.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -Wall -Wextra -MD -MQ main.p/greetings.c.o -MF main.p/greetings.c.o.d -o main.p/greetings.c.o -c ../greetings.c
[3/4] cc -o main main.p/main.c.o main.p/greetings.c.o -Wl,--as-needed -Wl,--no-undefinedNotice the line that starts with [1/4] or [2/4] we have those -Wall and -Wextra options in command line. But, it also give us warnings as well. Warning says, instead of passing -Wall and -Wextra use warning_level option. We can use this option by passing default_options to project() function instead of add_project_arguments().
project(
'hello-meson',
'c',
default_options: [
'warning_level=2'
]
)
sources = [
'main.c',
'greetings.c'
]
executable('main', sources)Note: If you sometime won’t see the output in terminal, you might need to delete buiddir folder and setup and compile again.
default_options is clean way to define the properties that you’re interested to pass in compilation process. warning_level=2 will be translated into -Wall and -Wextra for GCC and Clang compilers. This is same as to pass -Wall and -Wextra using add_project_arguments(). But, default_options is more portable and pass command options based on compiler than plain -Wall and -Wextra as it might possible Windows C/C++ compiler might have different options and Meson will take care of it.
Finally, let’s also select the C standard version with default_options only.
project(
'hello-meson',
'c',
default_options: [
'warning_level=2',
'c_std=c17'
]
)
sources = [
'main.c',
'greetings.c'
]
executable('main', sources)Let’s compile the project again.
meson compile -v -C builddirAnd we should see the output like this.
[1/3] ccache cc -Imain.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -std=c17 -O0 -g -MD -MQ main.p/main.c.o -MF main.p/main.c.o.d -o main.p/main.c.o -c ../main.c
[2/3] ccache cc -Imain.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -std=c17 -O0 -g -MD -MQ main.p/greetings.c.o -MF main.p/greetings.c.o.d -o main.p/greetings.c.o -c ../greetings.c
[3/3] cc -o main main.p/main.c.o main.p/greetings.c.o -Wl,--as-needed -Wl,--no-undefinedAs you can see in the above output we are seeing -std=c17 is passed in [1/3] and [2/3] commands.
Yay!