-
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 C/C++ Compiler, from Visual Studio Code on Windows.
Below you will find the software and their versions used in this guide. Newer versions might work with little or no modification(s).
Software | Version | Link |
---|---|---|
IAR Embedded Workbench for Arm | 9.50.1 | link |
Microsoft Visual Studio Code | 1.82.0 | link |
IAR Debug Extension | 1.30.2 | link |
Microsoft C/C++ Extension Pack | 1.3.0 | link |
Microsoft CMake Tools Extension | 1.15.31 | link |
CMake | 3.27.4 | link |
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 VSCode 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.c
source 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.txt
file with the following content:
cmake_minimum_required(VERSION 3.27)
# 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
--semihosting
--map .
--config "${TOOLKIT_DIR}/config/linker/ST/stm32f407xG.icf"
)
- Create a
<proj-dir>/.vscode/cmake-kits.json
file with the following content (adjusting the paths as needed):
[
{
"name": "IAR",
"compilers": {
"C": "C:/Program Files/IAR Systems/Embedded Workbench 9.3/arm/bin/iccarm.exe"
},
"cmakeSettings": {
"CMAKE_BUILD_TYPE": "Debug",
"TOOLKIT_DIR": "C:/Program Files/IAR Systems/Embedded Workbench 9.3/arm",
"CMAKE_MAKE_PROGRAM": "C:/Program Files/IAR Systems/Embedded Workbench 9.3/common/bin/ninja.exe"
}
}
]
Note
- CMake Tools will prefer Ninja if 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 from the drop-down list.
- Invoke the palette
- Perform CMake: Build.
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.
This header file can be generated with the following command:
mkdir %APPDATA%/Code/IAR
"C:/Program Files/IAR Systems/Embedded Workbench 9.3/arm/bin/iccarm.exe" . --c++ --predef_macros=n %APPDATA%/Code/IAR/iccarm_predef.h
Create this header file with the following contents:
#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
Note Keywords available as per IAR C/C++ Compiler for Arm 9.50.1
The following JSON file configures Microsoft Intellisense to work with the IAR C/C++ Compiler for Arm:
{
// Intellisense settings
"C_Cpp.default.compilerPath": "",
"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.systemIncludePath": [
"C:/Program Files/IAR Systems/Embedded Workbench 9.3/arm/inc/c",
"C:/Program Files/IAR Systems/Embedded Workbench 9.3/arm/inc/c/aarch32"
],
"C_Cpp.default.forcedInclude": [
"${env:APPDATA}/Code/IAR/iccarm_predef.h",
"${env:APPDATA}/Code/IAR/iccarm_keywords.h"
],
// CMake settings
"cmake.enabledOutputParsers": [
"iar",
"cmake"
]
}
Now we need to setup the launch configuration for the IAR C-SPY Debugger at <proj-dir>/.vscode/launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"type": "cspy",
"name": "C-SPY: Simulator",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"stopOnEntry": true,
"workbenchPath": "C:/Program Files/IAR Systems/Embedded Workbench 9.3",
"target": "arm",
"driver": "Simulator",
"driverOptions": [
"--cpu=Cortex-M4",
"--semihosting"
]
}
]
}
Once the 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, build it with the IAR Embedded Workbench or the IAR Build Tools and debug it with the IAR C-SPY Debugger.
This is the cmake-tutorial wiki. Back to Wiki Home
- Setting language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities