Building Zephyr within a development container in Visual Code

In previous articles (1) and (2) we have described the toolchain and the configuration of a Zephyr project. In this article we will bring everything together and use them from the Visual Code editor.

All code used in this article can be retrived from Zellacos github project.

 Visual Code has a nice feature called development containers that makes it possible to run the editor inside a Docker environment. By adding a folder with the name .devcontainer to you project in Visual Code and inside that folder there should be two files; devcontainer.json and Dockerfile.

The Dockerfile is based on the SDK image described in (2) with the addition of a user named dev that is given sudo privileges.

The devcontainer.json file is a Visual Code specific file that describes how the IDE shall build and start the docker image. In this configuration file we specify that the user shall be dev and also configures the ZEPHYR_BASE environment variable.

Next step is to start the container inside Visual Code. As a prerequisite to that Docker need to be installed in your system. When that is done the command “Dev Containers: Reopen in Container” shall be executed inside Visual Code under “View->Command Palette….”. This starts a Docker build command of the image specified by the Dockerfile and when the build is ready a container is created. Finally Visual Code connects to it and install the workspace file system into Visual code. Everything that is done inside Visual Code after that is done inside the context of the container. You can see that by starting a terminal inside Visual Code which will give you a small Ubuntu system connected to your workspace.

When your environment is up running you can install and build Zephyr as described in (1).

west init -l manifest-repo 
west update 

Which downloads the project sources. Finally, to build and run the example issue the following command

west build -b native_posix -t run zephyrblog_example1  

Looking further into the project structure you can find the .vscode folder. Inside this folder there is a file called c_cpp_properties.json. This file configure the ‘content-assist’ functionality of Visual Code and gives the developer a way to easy find Zephyr code references. There is also a tasks.json file that defines shortcuts to common command like update and build. These tasks can be executed from “Run->Run Tasks…” menu. Both of these files can be easily extended with your own commands or settings.

Previous
Previous

Meet us in Linköping!

Next
Next

Först på plats i Linköping!