-
Notifications
You must be signed in to change notification settings - Fork 12
Setting Build Flags
Since Arduino-CMake is kind of a build system for Arduino programs, it can also benefit from build flags to set the build's "tone".
Build Flags modify the output of the build for specific purposes that usually don't exist by default.
CMake allows developers to define custom build flags by setting a special variable CMAKE_[LANG]_FLAGS
, where [LANG] is the language you set the flags for (For example C, CXX (C++), ...).
There are 2 kinds of build flags: Compile Flags and Linker Flags
These define the compiler's behavior, which in our case is avr-gcc
.
To see the list of all possible flags that could be defined for this compiler, please see avr-gcc's man page.
Currently, the following variables can be defined:
-
ARDUINO_C_FLAGS
- C specific compiler flags -
ARDUINO_CXX_FLAGS
- C++ specific compiler flags -
ARDUINO_ASM_FLAGS
- Assembly specific compiler flags
You can set multiple variables if your project defined multiple programs in different languages, or you'd simply like to "mix" the flags to create a specialized build output.
If any of these variables is to be used, they must be defined before calling CMake's project()
function, which defined the project's name and its languages.
Build flags are simply strings that have special meaning to the compiler at hand, meaning you can pass lots of them in a single build process.
To do that, you would write all desired flags inside quotes (" "
) with a space between each flag.
For example, you can instruct the compiler to:
- Ignore exception handling symbols
- Disable hardware interrupts
By setting the following flags:
set(ARDUINO_CXX_FLAGS "-fno-exceptions -mno-interrupts")
These define the linker's behavior, which in our case is avr-ld
.
To see the list of all possible flags that could be defined for this compiler, please see avr-ld's man page.
Same as for the Compiler Flags, except the variables. For linker flags, the following variables are available:
-
ARDUINO_LINKER_FLAGS
- Linker's flags
Same as for the Compiler Flags, linker flags are just strings. The only main difference is that the flags are not separated by a space, but instead by a comma.
For example, you can instruct the linker to:
- Enable indirect invocation by a compiler driver such as gcc (As is always used in Arduino-CMake)
- Enable garbage collection of unused input sections.
By setting the following variables:
set(ARDUINO_LINKER_FLAGS "-Wl,--gc-sections")