| 1 |
--- |
| 2 |
short-description: Simple project step by step explanation |
| 3 |
... |
| 4 |
|
| 5 |
# Meson sample |
| 6 |
|
| 7 |
A Meson file that builds an executable looks like this. |
| 8 |
|
| 9 |
```meson |
| 10 |
project('simple', 'c') |
| 11 |
executable('myexe', 'source.c') |
| 12 |
``` |
| 13 |
|
| 14 |
All Meson build definitions begin with the `project` command. It |
| 15 |
specifies the name of the project and what programming languages it |
| 16 |
uses. Here the project is called *simple* and it uses only the C |
| 17 |
programming language. All strings are single-quoted. |
| 18 |
|
| 19 |
On the next line we define a *build target*, in this case an |
| 20 |
executable called *myexe*. It consists of one source file. This is all |
| 21 |
the code that a user needs to write to compile an executable with |
| 22 |
Meson. |
| 23 |
|
| 24 |
Variables are fully supported. The above code snippet could also have |
| 25 |
been declared like this. |
| 26 |
|
| 27 |
```meson |
| 28 |
project('simple', 'c') |
| 29 |
src = 'source.c' |
| 30 |
executable('myexe', src) |
| 31 |
``` |
| 32 |
|
| 33 |
Most executables consist of more than one source file. The easiest way |
| 34 |
to deal with this is to put them in an array. |
| 35 |
|
| 36 |
```meson |
| 37 |
project('simple', 'c') |
| 38 |
src = ['source1.c', 'source2.c', 'source3.c'] |
| 39 |
executable('myexe', src) |
| 40 |
``` |
| 41 |
|
| 42 |
Meson also supports the notion of *keyword arguments*. Indeed most |
| 43 |
arguments to functions can only be passed using them. The above |
| 44 |
snippet could be rewritten like this. |
| 45 |
|
| 46 |
```meson |
| 47 |
project('simple', 'c') |
| 48 |
src = ['source1.c', 'source2.c', 'source3.c'] |
| 49 |
executable('myexe', sources : src) |
| 50 |
``` |
| 51 |
|
| 52 |
These two formats are equivalent and choosing one over the other is |
| 53 |
mostly a question of personal preference. |
| 54 |
|
| 55 |
The `executable` command actually returns an *executable object*, |
| 56 |
which represents the given build target. It can be passed on to other |
| 57 |
functions, like this. |
| 58 |
|
| 59 |
```meson |
| 60 |
project('simple', 'c') |
| 61 |
src = ['source1.c', 'source2.c', 'source3.c'] |
| 62 |
exe = executable('myexe', src) |
| 63 |
test('simple test', exe) |
| 64 |
``` |
| 65 |
|
| 66 |
Here we create a unit test called *simple test*, and which uses the |
| 67 |
built executable. When the tests are run with the `meson test` |
| 68 |
command, the built executable is run. If it returns zero, the test |
| 69 |
passes. A non-zero return value indicates an error, which Meson will |
| 70 |
then report to the user. |
| 71 |
|
| 72 |
A note to Visual Studio users |
| 73 |
----- |
| 74 |
|
| 75 |
There's a slight terminology difference between Meson and Visual |
| 76 |
Studio. A Meson *project* is the equivalent to a Visual Studio |
| 77 |
*solution*. That is, the topmost thing that encompasses all things to |
| 78 |
be built. A Visual Studio *project* on the other hand is the |
| 79 |
equivalent of a Meson top level build target, such as an executable or |
| 80 |
a shared library. |