-
Notifications
You must be signed in to change notification settings - Fork 18
Building and Debugging from Visual Studio Code
This article will demonstrate the steps required for setting up a CMake project for an Arm Cortex-M4 device, built with the IAR Build Tools on Linux, from Visual Studio Code on Linux.
Below you will find the software and their versions used in this article. Newer versions might work with little or no modification(s).
| Software | Version | Link |
|---|---|---|
| Ubuntu Desktop | 24.04 | link |
| IAR Build Tools for Arm | 9.70.1 | link |
| Microsoft Visual Studio Code (VS Code) | 1.105.1 | link |
| Microsoft C/C++ Extension Pack | 1.3.1 | link |
| IAR Debug Extension | 1.42.1 | link |
| CMake | 4.1.2 | link |
Install the required software:
- Install the IAR Build Tools:
sudo apt install /path/to/bxarm-*.deb(setup the license) - Install CMake:
sudo apt install cmake - Install VS Code:
sudo apt install /path/to/vscode-*.deb - Launch VS Code:
code - Install the VS Code extensions (Ctrl+Shift+X):
This demonstration assumes all the required software installed with their defaults and ready to use.
- In Visual Studio Code, select:
File→Open Folder...(Ctrl+K, Ctrl+O).
Note VS Code will ask you if you trust the authors of the files in the opened folder.
-
Create a new empty folder. (e.g., "
hello"). This folder will be referred to as<proj-dir>from now on. -
Create a
<proj-dir>/main.csource file with the following content:
#include <intrinsics.h>
#include <stdio.h>
#include <stdint.h>
__root uint_fast8_t counter = 0;
void main() {
while (counter < 10) {
printf("Hello world! %u\n", counter);
++counter;
}
for(;;) {
++counter;
}
}- Create a
<proj-dir>/CMakeLists.txtfile with the following content:
cmake_minimum_required(VERSION 4.1.2)
# Set the project name and its required languages (ASM, C and/or CXX)
project(example LANGUAGES C)
# Add an executable named "hello"
add_executable(hello)
# Add "hello" source file(s)
target_sources(hello PRIVATE main.c)
# Set the compiler options for "hello"
target_compile_options(hello PRIVATE
--cpu Cortex-M4
-e
)
# Set the linker options for "hello"
target_link_options(hello PRIVATE
--config "${TOOLKIT_DIR}/config/linker/ST/stm32f407xG.icf"
--semihosting
--map .
)- Create a
<proj-dir>/.vscode/cmake-kits.jsonfile with the following content (adjusting the paths as needed):
[
{
"name": "IAR BXARM",
"compilers": {
"C": "/opt/iarsystems/bxarm/arm/bin/iccarm",
"CXX": "/opt/iarsystems/bxarm/arm/bin/iccarm",
"ASM": "/opt/iarsystems/bxarm/arm/bin/iasmarm"
},
"cmakeSettings": {
"TOOLKIT_DIR": "/opt/iarsystems/bxarm/arm",
"CMAKE_MAKE_PROGRAM": "/opt/iarsystems/bxarm/common/bin/ninja"
}
}
]Note
- CMake Tools will prefer Ninja it is present, unless configured otherwise.
- If you change the active kit while a project is configured, the project configuration will be re-generated with the chosen kit.
- Invoke the palette (CTRL+SHIFT+P).
- Perform CMake: Configure.
- Select IAR BXARM from the drop-down list.
- Invoke the palette
- Perform CMake: Build.
Output:
[main] Building folder: hello
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/user/hello/build --config Debug --target all --
[build] [1/2 50% :: 0.070] Building C object CMakeFiles/hello.dir/main.c.o
[build] [2/2 100% :: 0.144] Linking C executable hello.elf
[driver] Build completed: 00:00:00.206
[build] Build finished with exit code 0
In order to get accurate results, Intellisense needs:
- the compiler's internal macros
- the compiler's keywords
For consistency, we will create two header files containing such information. Open a new terminal and use the following commands:
##### ~/.config/Code/IAR/iccarm_predef.h
mkdir -p ~/.config/Code/IAR
## For pure C++ projects, invoke the commented command (with the `--c++` option)
# /opt/iarsystems/bxarm/arm/bin/iccarm . --c++ --predef_macros=n ~/.config/Code/IAR/iccarm_predef.h
/opt/iarsystems/bxarm/arm/bin/iccarm . --predef_macros=n ~/.config/Code/IAR/iccarm_predef.h
##### ~/.config/Code/IAR/iccarm_keywords.h:
cat << EOF > ~/.config/Code/IAR/iccarm_keywords.h
#define __fp16 float,
#define __constrange(...)
#define __c99_generic(...)
#define __spec_string
#define __data
#define __func__ ""
#define __alignof__(a) 1
#define __ALIGNOF__ __alignof__
#define __section_begin(...) ((void*)0)
#define __section_end(...) ((void*)0)
#define __section_size(...) ((size_t)0)
#define __segment_begin(...) ((void*)0)
#define __segment_end(...) ((void*)0)
#define __segment_size(...) ((size_t)0)
#define __DATA_MEMORY_LIST1__()
#define __absolute
#define __arm
#define __big_endian
#define __cmse_nonsecure_call
#define __cmse_nonsecure_entry
#define __exception
#define __fiq
#define __interwork
#define __intrinsic
#define __irq
#define __little_endian
#define __naked
#define __no_alloc
#define __no_alloc16
#define __no_alloc_str
#define __no_alloc_str16
#define __nested
#define __no_init
#define __noreturn
#define __nounwind
#define __packed
#define __pcrel
#define __ramfunc
#define __root
#define __ro_placement
#define __sbrel
#define __stackless
#define __svc
#define __swi
#define __task
#define __thumb
#define __weak
EOFNote Extended keywords available as per IAR C/C++ Compiler for Arm 9.70.1
The following JSON file configures Microsoft Intellisense to work with the IAR C/C++ Compiler for Arm:
{
// Intellisense settings
"C_Cpp.default.compilerPath": "/opt/iarsystems/bxarm/arm/bin/iccarm",
"C_Cpp.default.cStandard": "c17",
"C_Cpp.default.cppStandard": "c++17",
"C_Cpp.default.intelliSenseMode": "clang-arm",
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"C_Cpp.default.mergeConfigurations": true,
"C_Cpp.default.includePath": [
"/opt/iarsystems/bxarm/arm/inc/c",
"/opt/iarsystems/bxarm/arm/inc/c/aarch32"
],
"C_Cpp.default.forcedInclude": [
"~/.config/Code/IAR/iccarm_predef.h",
"~/.config/Code/IAR/iccarm_keywords.h"
],
// CMake settings
"cmake.enabledOutputParsers": ["cmake", "iar"]
}The file can be saved in the project folder as .vscode/settings.json or in the location for user-wide scope ~/.config/Code/User/settings.json.
In this final section, it is time to setup the project's launch configuration for the IAR C-SPY Simulator for Arm in Visual Studio Code. For this article, we will use a Cortex-M4 simulated target.
- Go to Run → Add Configuration...
- From the drop-down list, select IAR C-SPY Debug
- From the next drop-down list, select Template: Debug with C-SPY Simulator (Arm template)
A general template will be created inside the project folder (<proj-dir>/.vscode/launch.json). Update the fields in the template configuration to match the following:
{
// Use IntelliSense to learn about possible attributes.
// Nover to view description of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "cspy",
"request": "launch",
"name": "Debug with C-SPY Simulator",
"target": "arm",
"program": "${command:cmake.launchTargetPath}",
"stopOnSymbol": "main",
"workbenchPath": "/opt/iarsystems/bxarm",
"projectPath": "${workspaceRoot}",
"driver": "Simulator",
"driverOptions": [
"--cpu=cortex-m4",
"--semihosting"
]
}
]
}Once the <proj-dir>/build/hello.elf executable was built:
- In
main.c, click on the left curb of the line which increments the counter (++counter;) to set a breakpoint. - Go to Run → Start Debugging (F5) to start the debugging session.
This article described the steps to prepare a CMake project in Visual Studio Code on Linux, build it with the IAR Build Tools and debug it with the IAR C-SPY Debugger.
This is the cmake-tutorial wiki. Back to Wiki Home
- IAR Compiler options in a CMake project
- IAR ILINK options in a CMake project
- Language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities
- Use the IAR ELF Tool to convert executable targets to their binary formats
- Using IAR Build Tools with CMake Presets