Debian Codegraph @

subprojects/meson-docs/docs/markdown/Adding-arguments.md

raw
1 ---
2 short-description: Adding compiler arguments
3 ...
4
5 # Adding arguments
6
7 Often you need to specify extra compiler arguments. Meson provides two
8 different ways to achieve this: global arguments and per-target
9 arguments.
10
11 Global arguments
12 --
13
14 Global compiler arguments are set with the following command. As an
15 example you could do this.
16
17 ```meson
18 add_global_arguments('-DFOO=bar', language : 'c')
19 ```
20
21 This makes Meson add the define to all C compilations. Usually you
22 would use this setting for flags for global settings. Note that for
23 setting the C/C++ language standard (the `-std=c99` argument in GCC),
24 you would probably want to use a default option of the [[project]]
25 function. For details see the [reference manual](Reference-manual.md).
26
27 Global arguments have certain limitations. They all have to be defined
28 before any build targets are specified. This ensures that the global
29 flags are the same for every single source file built in the entire
30 project with one exception. Compilation tests that are run as part of
31 your project configuration do not use these flags. The reason for that
32 is that you may need to run a test compile with and without a given
33 flag to determine your build setup. For this reason tests do not use
34 these global arguments.
35
36 You should set only the most essential flags with this setting, you
37 should *not* set debug or optimization flags. Instead they should be
38 specified by selecting an appropriate build type.
39
40 Project arguments
41 --
42
43 Project arguments work similar to global arguments except that they
44 are valid only within the current subproject. The usage is simple:
45
46 ```meson
47 add_project_arguments('-DMYPROJ=projname', language : 'c')
48 ```
49
50 This would add the compiler flags to all C sources in the current
51 project.
52
53 Per target arguments
54 --
55
56 Per target arguments are just as simple to define.
57
58 ```meson
59 executable('prog', 'prog.cc', cpp_args : '-DCPPTHING')
60 ```
61
62 Here we create a C++ executable with an extra argument that is used
63 during compilation but not for linking.
64
65 You can find the parameter name for other languages in the [reference
66 tables](Reference-tables.md).
67
68 Specifying extra linker arguments is done in the same way:
69
70 ```meson
71 executable('prog', 'prog.cc', link_args : '-Wl,--linker-option')
72 ```