Skip to content
systemcoding.dev
RTOS

How a Zephyr Application Is Structured: CMakeLists, prj.conf and Boards

A Zephyr application is a CMakeLists.txt, a prj.conf, a src folder and optional board files. See what each file does and how a build combines them.

6 min read
Summarize in AI
Flat illustration of RTOS tasks taking turns on a timeline, with the C, Rust and Espressif logos.

A Zephyr application is smaller than most newcomers expect. Three files are enough to build and run a program: a CMakeLists.txt, a prj.conf and one C source file. Everything else, from the kernel to the drivers to the board description, comes from the Zephyr tree and is pulled in by a single CMake call. This post names every file an application can contain, says which ones are required, and explains what the build does with them in Zephyr 4.2. It is the first rung of the Zephyr basics ladder, which the complete Zephyr devicetree and Kconfig guide builds on.

The smallest application

Create a folder with this layout:

Text
blinky-min/
├── CMakeLists.txt
├── prj.conf
└── src/
    └── main.c

The CMakeLists.txt is the only file that talks to CMake directly:

CMakeLists.txtcmake
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky_min)

target_sources(app PRIVATE src/main.c)

find_package(Zephyr ...) must come before project(). That one call loads the Zephyr build system: it reads the board, evaluates devicetree and Kconfig, and defines a CMake target named app. The project() line names your project, and target_sources(app PRIVATE ...) adds your source files to the app target. The HINTS $ENV{ZEPHYR_BASE} part tells CMake where the Zephyr tree is when you have sourced zephyr-env.sh or activated a west workspace. Zephyr 4.2 requires CMake 3.20.5 or newer, which is why the minimum version line says 3.20.0.

The prj.conf holds Kconfig assignments, one per line:

prj.confText
CONFIG_PRINTK=y
CONFIG_LOG=n

And src/main.c is an ordinary C file with a main() function:

src/main.cc
#include <zephyr/kernel.h>

int main(void)
{
	printk("Hello from %s\n", CONFIG_BOARD_TARGET);
	return 0;
}

Building for the qemu_cortex_m3 board is one command, and the program prints:

Text
*** Booting Zephyr OS build v4.2.0 ***
Hello from qemu_cortex_m3

The first line is printed by the kernel at boot, and the second by your main(). The board string comes from CONFIG_BOARD_TARGET, which Zephyr sets from the -b argument.

What each file does

File or folderRequiredRole
CMakeLists.txtYesLoads Zephyr, names the project, lists sources
prj.confNo, but almost always presentKconfig options for every board
src/By conventionApplication sources, listed in CMakeLists.txt
boards/NoPer-board overlays and config fragments
KconfigNoYour own Kconfig symbols
sample.yamlNoMetadata for the Twister test runner
west.ymlNoWorkspace manifest, only in a workspace application

Only CMakeLists.txt is mandatory, since a project with no prj.conf builds with the board's default configuration. The official reference for the whole layout is the application development page in the Zephyr documentation.

How prj.conf becomes the final configuration

Kconfig symbols resolve in layers, and a later layer wins. The build starts from the architecture and SoC defaults, applies the board's <board>_defconfig, then merges your prj.conf, and finally merges any file listed in EXTRA_CONF_FILE or found in boards/. The merged result is written to build/zephyr/.config, and the C header build/zephyr/include/generated/zephyr/autoconf.h turns each symbol into a macro your code can test.

A symbol you set in prj.conf that Kconfig cannot honor does not fail silently. If you write CONFIG_GPIO=y and the selected board has no GPIO driver, the build emits a warning that the assignment "was assigned the value y, but got the value n". That message is the first thing to look for when a feature seems to be ignored. Chapter-length treatment of the layering lives in the Kconfig explainer, and the devicetree and Kconfig guide covers the tooling.

Where boards fit in

A board is not part of your application. It lives in the Zephyr tree, or in a module, as a folder under boards/<vendor>/<board>/. It supplies a devicetree source file that describes the hardware, a defconfig that turns on the drivers the board needs, and CMake code that tells west flash how to program it. You choose one at build time with -b:

Shell
west build -b nrf52840dk/nrf52840 blinky-min

In Zephyr 4.2 the argument is a board target, which is the board name followed by an optional SoC and CPU cluster, separated by slashes. This is hardware model v2, and it replaced the older single-word names like nrf52840dk_nrf52840. A board revision goes after an @, as in [email protected]/nrf52840.

Your application changes a board's behavior without editing it. You do that with overlays, which are devicetree and Kconfig fragments stored in your own boards/ folder. For the board above, a file named boards/nrf52840dk_nrf52840.overlay is picked up automatically, and boards/nrf52840dk_nrf52840.conf is merged into the configuration. Naming follows the board target with the slashes turned into underscores.

Application types

Zephyr documents three places an application can live, and the choice changes only where ZEPHYR_BASE comes from:

  1. Repository application. The folder sits inside the Zephyr repository, as the samples do. It is the quickest way to experiment, and the wrong place to keep real work, because upgrading Zephyr touches the same tree.

  2. Workspace application. The folder sits next to the Zephyr repository inside a west workspace, with a west.yml that pins versions of Zephyr and any modules.

  3. Freestanding application. The folder sits anywhere, and you point ZEPHYR_BASE at a Zephyr checkout. This is the simplest to start, and the hardest to reproduce on another machine.

Choosing between them is the subject of separate posts on standalone applications and west manifests. For learning, the repository layout is fine.

What a build produces

Running west build -b qemu_cortex_m3 blinky-min creates a build/ folder. The pieces worth knowing are:

Path in build/What it is
zephyr/zephyr.elfThe linked image, with debug symbols
zephyr/zephyr.bin, zephyr.hexThe same image as raw binary and Intel hex
zephyr/.configThe final merged Kconfig
zephyr/zephyr.dtsThe final merged devicetree
zephyr/include/generated/Generated headers, including autoconf.h and the devicetree macros

When something goes wrong, you debug by reading .config and zephyr.dts before reading any code, because they are what the compiler actually saw. The inspect-the-final-files post later in this ladder shows the full routine.

Mistakes that cost an afternoon

**Calling find_package after project.** CMake then has no Zephyr toolchain configured and reports errors about missing compilers. The order is fixed.

**Adding a source file and forgetting CMakeLists.txt.** Zephyr does not glob. A new src/uart_io.c is invisible to the build until you add it to target_sources, and the failure looks like an undefined reference at link time.

Reusing a build folder after changing the board. Passing a different -b to an existing build/ folder makes west warn that the board has changed. Use west build -p always -b <board> to start clean, or give each board its own folder with -d build/<board>.

**Editing build/zephyr/.config by hand.** The next build regenerates it from your sources. Put the change in prj.conf.

When this layout does not fit

The three-file layout is the right start, and it stops being the right layout when an application needs to build two images, such as a bootloader and a firmware, or when it shares code across many projects. Those cases use sysbuild and out-of-tree modules, covered in later posts. A project that only wants Zephyr's drivers and nothing else is also better served by checking whether a vendor SDK built on Zephyr already provides its own structure.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.