How to Use: meson

Install Link to heading

sudo pacman -S gcc meson ninja

Develop Link to heading

Write a sample program inside src/main.c:

#include <stdio.h>

int main(int argc, char **argv) {
  printf("Hello World!\n");

  return 0;
}

Configure Link to heading

Create a meson.build file containing:

project(
  'meson-sample',
  'c',
  version: '0.0.1'
)

executable('hello', 'src/main.c')

Set up your compile target config files:

  • compile/x86_64-generic.ini:
[target_machine]
system     = 'linux'
cpu_family = 'x86_64'
cpu        = 'x86'
endian     = 'little'

[binaries]
strip = 'strip'

[built-in options]
c_std  = 'c17'
c_args = '-march=x86-64 -mtune=generic'
  • compile/x86_64-znver1.ini:
[target_machine]
system     = 'linux'
cpu_family = 'x86_64'
cpu        = 'znver1'
endian     = 'little'

[binaries]
strip = 'strip'

[built-in options]
c_std  = 'c17'
c_args = '-march=znver1 -mtune=znver1'

Compile Link to heading

  1. First set up your build targets:
meson setup build/x86_64-generic \
  --buildtype=release            \
  --cross-file ./compile/x86_64-generic.ini

meson setup build/x86_64-znver1 \
  --buildtype=release           \
  --cross-file ./compile/x86_64-znver1.ini
  1. Then you can compile:
meson compile -C build/x86_64-generic
meson compile -C build/x86_64-znver1
  1. Now your binaries are ready to run:
time ./build/x86_64-generic/hello
# Hello World!
#
# real    0m0.002s
# user    0m0.001s
# sys     0m0.001s

time ./build/x86_64-znver1/hello
# Hello World!
#
# real    0m0.002s
# user    0m0.002s
# sys     0m0.000s

Aarch64 Link to heading

Cross compile for ARM with these extra steps:

  1. Install a cross-compiler:
sudo pacman -S aarch64-linux-gnu-gcc
  1. Set up your compile target config file:
  • compile/aarch64-generic.ini:
[target_machine]
system     = 'linux'
cpu_family = 'aarch64'
cpu        = 'aarch64'
endian     = 'little'

[binaries]
c     = 'aarch64-linux-gnu-gcc'
strip = 'strip'

[built-in options]
c_std  = 'c17'
c_args = '-march=armv8-a -mtune=cortex-a72'
  1. Set up your build target:
meson setup build/aarch64-generic \
  --buildtype=release             \
  --cross-file ./compile/aarch64-generic.ini
  1. Compile:
meson compile -C build/aarch64-generic
  1. Now you can run it, given you have, or transfer it to, a ARM CPU device:
time ./build/aarch64-generic/hello
# Hello World!
#
# real    0m0.002s
# user    0m0.002s
# sys     0m0.000s