Adding files and configuring CMake
As described in more detail in Build and configuration system, the Zephyr and nRF Connect SDK build systems are based on CMake.
For this reason, every application in Zephyr and the nRF Connect SDK must have a CMakeLists.txt
file.
This file is the entry point of the build system as it specifies the application source files for the compiler to include in the configuration phase.
Maintaining CMakeLists.txt
The recommended method to maintain and update the CMakeLists.txt
file is to use the nRF Connect for VS Code extension.
The extension provides support for the source control with west and CMake build system, including build configuration management and source and config files overview.
Adding source files to CMakeLists.txt
You can add source files to the app
CMake target with the target_sources()
function provided by CMake.
Pay attention to the following configuration options:
If your application is complex, you can split it into subdirectories. These subdirectories can provide their own
CMakeLists.txt
files. (The mainCMakeLists.txt
file needs to include those.)The build system searches for header files in include directories. Add additional include directories for your application with the
target_include_directories()
function provided by CMake. For example, if you want to include aninc
directory, the code would look like the following:target_include_directories(app PRIVATE inc)
See Application CMakeLists.txt in the Zephyr documentation for more information about how to edit CMakeLists.txt
.
Note
You can also read the CMake Tutorial in the CMake documentation for a better understanding of how CMakeLists.txt
are used in a CMake project.
This tutorial however differs from Zephyr and nRF Connect SDK project configurations, so use it only as reference.
Providing CMake options
You can provide additional options for building your application to the CMake process, which can be useful, for example, to switch between different build scenarios. These options are specified when CMake is run, thus not during the actual build, but when configuring the build.
For information about what variables can be set and how to add these variables in your project, see Important Build System Variables in the Zephyr documentation.
If you work with the nRF Connect for VS Code extension, you can specify project-specific CMake options when you add the build configuration for a new nRF Connect SDK project. See How to build an application in the nRF Connect for VS Code extension documentation.
If you work on the command line, pass the additional options to the west build
command when Configuring and building an application.
The options must be added after a --
at the end of the command.
See One-Time CMake Arguments for more information.