Modifying an application

For simplicity, this guide will refer to both samples and applications as “applications”.

After programming and testing an application, you probably want to make some modifications to the application, for example, add your own files with additional functionality, change compilation options, or update the default configuration.

Adding files and changing compiler settings

The nRF Connect SDK build system is based on Zephyr, whose build system is based on CMake. For more information about how the build system works in Zephyr, see Build and Configuration Systems and Application Development in the Zephyr documentation.

Role of CMakeLists.txt

In the nRF Connect SDK, the application is a CMake project. As such, the application controls the configuration and build process of itself, Zephyr, and sourced libraries. The application’s CMakeLists.txt file is the main CMake project file and the source of the build process configuration.

Zephyr provides a CMake package that must be loaded by the application into its CMakeLists.txt file. When loaded, the application can reference items provided by both Zephyr and the nRF Connect SDK.

Loading Zephyr’s CMake package creates the app CMake target. You can add application source files to this target from the application CMakeLists.txt file.

Updating CMakeLists.txt

The recommended method to update the CMakeLists.txt file is to use the nRF Connect for VS Code extension to maintain it. You can also edit the file directly.

Editing 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 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 an inc directory, the code would look like the following:

    target_include_directories(app PRIVATE inc)
    

See the CMake documentation and Build System (CMake) in the Zephyr documentation for more information about how to edit CMakeLists.txt.

Advanced compiler settings

The application has full control over the build process.

Using Zephyr’s configuration options is the standard way of controlling how the system is built. These options can be found under Zephyr’s menuconfig Build and Link Features > Compiler Options. For example, to turn off optimizations, select CONFIG_NO_OPTIMIZATIONS.

Compiler options not controlled by the Zephyr build system can be controlled through the CONFIG_COMPILER_OPT Kconfig option.

Visual Studio Code compiler settings

The nRF Connect for VS Code extension lets you build and program with custom options. For more information, read about the advanced Custom launch and debug configurations and Application-specific flash options in the extension documentation.

Configuring your application

You might want to change the default options of the application. There are different ways of doing this, but not all will store your configuration permanently.

Configuration system overview

Zephyr and the nRF Connect SDK use several configuration systems, each system with a specialized syntax and purpose.

The nRF Connect SDK consists of the following configuration sources:

  • Devicetree source (DTS) files for hardware-related options.

  • Kconfig files for software-related options.

  • Partition Manager files for memory layout configuration. This is an nRF Connect SDK configuration system that is not available in Zephyr.

To read more about Zephyr’s configuration system, see Build and Configuration Systems in the Zephyr documentation.

Memory layout configuration

The Partition Manager is specific to the nRF Connect SDK. If enabled, it provides the memory layout configuration. The layout is impacted by various elements, such as Kconfig configuration options or the presence of child images. Partition Manager ensures that all required partitions are in the correct place and have the correct size.

If enabled, the memory layout can be controlled in the following ways:

  • Dynamically (default) - In this scenario, the layout is impacted by various elements, such as Kconfig configuration options or the presence of child images. Partition Manager ensures that all required partitions are in the correct place and have the correct size.

  • Statically - In this scenario, you need to provide the static configuration. See Static configuration for information about how to do this.

After CMake has run, a single partitions.yml file with the complete memory layout will have been created in the build directory. This process also creates a set of header files that provides defines, which can be used to refer to memory layout elements.

For more information, see Partition Manager.

Child images

The nRF Connect SDK build system allows the application project to become a root for the sub-applications known in the nRF Connect SDK as child images. Examples of child images are bootloader images, network core images, or security-related images. Each child image is a separate application.

For more information, see Multi-image builds.

Changing the configuration temporarily

When building your application, the different .config, *_defconfig files and the prj.conf file are merged together and then processed by Kconfig. The resulting configuration is written to the zephyr/.config file in your build directory. This means that this file is available when building the application, but it is deleted when you clean the build directory with the pristine target (see Zephyr’s Rebuilding an Application for more information).

The documentation for each configuration option lists the menu path where the option can be found.

Use the nRF Kconfig GUI in the nRF Connect for VS Code extension to select the desired options. The GUI organizes the Kconfig options in a hierarchical list and lets you view and manage your selection.

To locate a specific configuration option, use the Search modules field. Read the Configuring with nRF Kconfig page in the nRF Connect for VS Code extension documentation for more information.

Changing the configuration permanently

To configure your application and maintain the configuration when you clean the build directory pristinely, you need to specify the configuration in one of the permanent configuration files. This will be either the default prj.conf file of the application or an extra Kconfig fragment. In these files, you can specify different values for configuration options that are defined by a library or board, and you can add configuration options that are specific to your application.

See Setting Kconfig configuration values in the Zephyr documentation for information on how to change the configuration permanently.

Tip

Reconfiguring through menuconfig only changes the specific setting and the invisible options that are calculated from it. It does not adjust visible symbols that have already defaulted to a value even if this default calculation is supposed to be dependent on the changed setting. This may result in a bloated configuration compared to changing the setting directly in prj.conf. See the section Stuck symbols in menuconfig and guiconfig on the Kconfig - Tips and Best Practices in the Zephyr documentation for more information.

If you work with nRF Connect for VS Code extension, you can use one of the following options:

  • Select an extra Kconfig fragment file when you build an application.

  • Edit the Kconfig options using the nRF Kconfig GUI and save changes permanently to an existing or new prj.conf file.

See the extension’s documentation about Kconfig for more information.

The prj.conf file is read when you open a project. The file will be reloaded when CMake re-runs. This will happen automatically when the application is rebuilt.

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.

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.

Configuring build types

When the CONF_FILE variable contains a single file and this file follows the naming pattern prj_<buildtype>.conf, then the build type will be inferred to be <buildtype>. The build type cannot be set explicitly. The <buildtype> can be any string, but it is common to use release and debug.

For information about how to set variables, see Important Build System Variables in the Zephyr documentation.

The Partition Manager’s static configuration can also be made dependent on the build type. When the build type has been inferred, the file pm_static_<buildtype>.yml will have precedence over pm_static.yml.

The child image Kconfig configuration can also be made dependent on the build type. The child image Kconfig file is named <child_image>.conf instead of prj.conf, but otherwise follows the same pattern as the parent Kconfig.

The Devicetree configuration is not affected by the build type.

Note

For an example of an application that is using build types, see the nRF Desktop application (nRF Desktop build types) or the nRF Machine Learning application (nRF Machine Learning build types).

To select the build type in the nRF Connect for VS Code extension:

  1. When building an application as described in the nRF Connect for VS Code extension documentation, follow the steps for setting up the build configuration.

  2. In the Add Build Configuration screen, select the desired .conf file from the Configuration drop-down menu.

  3. Fill in other configuration options, if applicable, and click Build Configuration.