Skip to content
systemcoding.dev
RTOS

Create a Standalone Zephyr Application Outside the Main Tree

Keep a Zephyr application in its own folder by setting ZEPHYR_BASE and finding Zephyr from CMake. Includes the layout, the build commands and the limits.

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

Every Zephyr sample you build while learning sits inside the Zephyr repository, and every real project eventually needs to leave it. A firmware that lives under zephyr/samples/ is tangled up with the upstream history, gets overwritten when you update the checkout, and cannot be put in its own Git repository without copying files. A standalone application fixes that by keeping your code in its own folder and pointing the build at a separate Zephyr checkout. This post builds one from an empty directory with Zephyr 4.2 and the Zephyr SDK 0.17, as a first step beyond the structure described in the post on how a Zephyr application is laid out.

What "standalone" means

Zephyr's documentation names three application locations: inside the Zephyr repository, inside a west workspace, and freestanding. A freestanding application is a folder anywhere on disk. It finds Zephyr through the ZEPHYR_BASE environment variable or through a CMake package registration, and it takes no part in Zephyr's Git tree. This post covers the freestanding case, because it needs no manifest and works with the simplest possible setup. The workspace case is the subject of a later post on west manifests.

The trade-off is reproducibility. A freestanding application records nothing about which Zephyr version it was built against, so a colleague with a different checkout can get a different result. For one person on one machine that is acceptable, and it is a poor basis for a team.

Step 1: get Zephyr once

You need one Zephyr checkout that every standalone application shares. Create it with west:

Shell
python3 -m venv ~/zephyrproject/.venv
source ~/zephyrproject/.venv/bin/activate
pip install west
west init ~/zephyrproject
cd ~/zephyrproject
west update
west zephyr-export
pip install -r zephyr/scripts/requirements.txt

west init creates the workspace and downloads the Zephyr repository into ~/zephyrproject/zephyr. west update fetches the modules it depends on, such as HALs. west zephyr-export registers Zephyr in the CMake user package registry, which lets find_package(Zephyr) find it later without any hints. The last command installs the Python packages the build scripts import. The Zephyr SDK, which provides the compilers, is installed separately from the getting started guide, and its own setup script registers it the same way.

Step 2: create the application folder

Choose any directory that is not inside ~/zephyrproject:

Text
~/firmware/sensor-hub/
├── CMakeLists.txt
├── prj.conf
└── src/
    └── main.c

The CMakeLists.txt is identical to the in-tree version, with no relative paths into Zephyr:

CMakeLists.txtcmake
cmake_minimum_required(VERSION 3.20.0)

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

target_sources(app PRIVATE src/main.c)

The HINTS $ENV{ZEPHYR_BASE} argument gives CMake a direct path when the variable is set. Without the variable, CMake falls back to the registry entry written by west zephyr-export, so either one is enough.

prj.confText
CONFIG_PRINTK=y
src/main.cc
#include <zephyr/kernel.h>

int main(void)
{
	while (1) {
		printk("sensor-hub alive\n");
		k_sleep(K_SECONDS(1));
	}
	return 0;
}

The loop prints a line once a second. k_sleep(K_SECONDS(1)) suspends the main thread for one second and lets the idle thread run.

Step 3: tell west where Zephyr is

West commands such as west build are extension commands, and they only exist when west is run from inside a workspace. A freestanding application is not inside one, so you have two options.

Option A: run west from the workspace. Change into ~/zephyrproject and give the application path:

Shell
cd ~/zephyrproject
west build -b qemu_cortex_m3 ~/firmware/sensor-hub -d ~/firmware/sensor-hub/build

The -d flag puts the build folder next to the application instead of inside the workspace.

Option B: set the environment. Source the Zephyr environment script, which sets ZEPHYR_BASE, and run west from your application folder:

Shell
source ~/zephyrproject/zephyr/zephyr-env.sh
cd ~/firmware/sensor-hub
west build -b qemu_cortex_m3

West finds the workspace through ZEPHYR_BASE, and the default build folder is build/ inside the application. With option B you can run west build, west flash and west debug from the application directory as usual.

Either way the program prints this after the kernel banner:

Text
*** Booting Zephyr OS build v4.2.0 ***
sensor-hub alive
sensor-hub alive
sensor-hub alive

The banner appears once, and the loop line repeats each second until you stop the emulated board. These lines are shown as the code would print them, and the version string will match whichever tag your checkout has.

Step 4: put it under version control

The application folder is now an ordinary Git repository candidate. Add a .gitignore that excludes build/, and commit the rest:

.gitignoreText
build/

What belongs in the repository is the source, prj.conf, CMakeLists.txt, and any boards/ overlays. What does not belong is a copy of Zephyr. Write the Zephyr version you used into a README, because nothing else records it. When you outgrow that, the west manifest in a workspace application records the revision for you.

Keep the environment out of the build

Freestanding builds are sensitive to what is in your shell. Three variables matter:

VariableUsed forIf it is wrong
ZEPHYR_BASEPath to the Zephyr repositoryThe build uses a different Zephyr than you expect
ZEPHYR_TOOLCHAIN_VARIANTWhich toolchain family to use, such as zephyrCMake cannot find a compiler
ZEPHYR_SDK_INSTALL_DIRLocation of the Zephyr SDKThe SDK is not found, or an old one is

Print them before a confusing build, and clear any that you did not set on purpose. The first line of CMake output also prints the Zephyr base it chose, which is the quickest check.

When a standalone application is the wrong choice

Use a workspace application when more than one person builds the project, when you track a specific Zephyr release, or when you depend on modules beyond the default set. Use the in-tree layout only for experiments you intend to throw away. A freestanding application is the right choice for a personal project, a learning exercise, or a quick comparison of two Zephyr versions by switching ZEPHYR_BASE.

Checking that the right Zephyr was used

Because nothing in a freestanding application names a Zephyr version, make the build tell you. The first lines of CMake output print the location it resolved, and west build also prints a line naming the Zephyr base. Compare that path with the checkout you meant to use. A mismatch usually means an old ZEPHYR_BASE is exported in your shell profile, left over from a previous project.

You can also ask the build for the version it linked. The KERNEL_VERSION_STRING macro is available from zephyr/version.h, so a one-line printk("%s\n", KERNEL_VERSION_STRING); at startup records the version in every log you capture. Adding it to a freestanding application is cheap insurance, since the repository itself records nothing about the version.

Moving the same application between two Zephyr versions

A freestanding layout makes version comparisons easy. Keep two checkouts, for example ~/zephyr-4.1 and ~/zephyr-4.2, each with its own workspace and virtual environment. Switch by sourcing the matching zephyr-env.sh in a fresh shell and building into a separate directory:

Shell
source ~/zephyr-4.1/zephyr/zephyr-env.sh
west build -b qemu_cortex_m3 -d build/z41
source ~/zephyr-4.2/zephyr/zephyr-env.sh
west build -b qemu_cortex_m3 -d build/z42

Use a new shell for each, because sourcing the second script does not unset everything the first one set. Comparing the two outputs shows which of your warnings are new, which is a fast way to learn what an upgrade will cost before you commit to it.

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.