The previous parts were about installation, project setup, building, debugging and setting up a kit. This one is about setting up IntelliSense for Cross Development in Visual Studio Code which allows for browsing symbols or code completion:

Outline
The powerful IntelliSense in Visual Studio Code is probably *the* feature in Visual Studio Code. The Eclipse IDE has a similar feature with the ‘Indexer‘, but imho the one in Visual Studio Code is not only easier to setup but more powerful.
If you are unfamiliar what IntelliSense does, check out https://code.visualstudio.com/docs/editor/intellisense

Setup
As everything else, the Indexer in Visual Studio Code is using a JSON configuration file. With the Microsoft C/C++ Extension there is a graphical (UI) configuration utility. Use <CTRL>+<P>:

In the following UI all the settings can be configured:

The settings are stored inside the .vscode/c_cpp_properties.json file inside the project. So instead using the wizard above simply create the file and copy-paste the template below:
{
"configurations": [
{
"name": "CrossARM",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"CPU_MK22FN512VLH12",
"CPU_MK22FN512VLH12_cm4"
],
"forcedInclude": ["${workspaceFolder}/src/IncludeMcuLibConfig.h"],
"compilerPath": "C:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2020-q4-major/bin/arm-none-eabi-gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-arm"
}
],
"version": 4
}
- name: arbitrary name for the configuration
- includePath: list here all locations where IntelliSense shall search for header files. It automatically will use the ones located in the toolchain (compiler) installation. The ‘**’ notation means it will search recursively that folder
- defines: list all the extra defines which shall be considered.
- forcedInclude: if using the -include compiler option, make sure you list that file here
- compilerPath: path to the compiler executable.
- cStandard and cppStandard: the C and C++ standard to be used
- intelliSenseMode: mode to be used. ‘gcc-arm’ is considered legacy, but not sure what to use otherwise for ARM?
Usage
With this IntelliSense is able to find the correct header files and definitions with the toolchain used. You can verify this with the context menu on a standard library include (Context menu > Peek > Definition):

Code Completion and IntelliSense in general is triggered by <CTRL>+<SPACE>:

You can press <CTRL> and then use the mouse to jump to a location: press <CTRL>, move the mouse over it and click.
Another nice thing is the ‘sneak’ preview: keep <CTRL> pressed and move the mouse around to see a preview of that implementation:

Another tip is to use <TAB>: it will select a ‘best match’ from the list of proposals.

What makes it nice too is its awareness for Doxygen documentation in the sources:

Summary
Visual Studio Code comes with a powerful IntelliSense engine which is easily configured using a JSON file. IntelliSense and Code Completion makes coding so much easier: it is *the* reason why to use an IDE like Visual Studio Code.
Happy Completing 🙂
Links
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 1
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 2
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 3
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 4
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 5
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 6
- Visual Studio Code for C/C++ with ARM Cortex-M: Part 6 – FreeRTOS
- Visual Studio Code website: https://code.visualstudio.com/
- IntelliSense: https://code.visualstudio.com/docs/editor/intellisense
- Intellisense for Cross Development: https://code.visualstudio.com/docs/cpp/configure-intellisense-crosscompilation
- Examples on GitHub: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/VisualStudioCode
Emphasis on *the* reason! You are absolutely right about what makes VC so attractive. Thanks for explaining how to set it up. I also wondered where to put my #defines.
LikeLiked by 1 person
Initially I struggled about finding the right place for the #defines too. I was hoping that as in Eclipse the IDE would be able to know them automatically from the build settings. Or at least I have not found how to make this work. Anyway, I’m fine to have it added ‘by hand’ as this is transparent and easy to do, and only more initial setup time.
You can find a more complete project here: https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/VisualStudioCode/FRDM-K22F/FRDM-K22F_Blinky
LikeLike
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 2 | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 3 | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 4 | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 5 | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 1 | MCU on Eclipse
Hi Erich
Ideally you don’t need to set any of these includePath/defines/forcedInclude but set compileCommands which is a json file generated by cmake. that file defines the exact compiler invocation for each file. so if cmake/ninja is able to build your project, intellisense will get exactly the same configuration…
LikeLiked by 2 people
Hi Martin,
yes, this was my thinking too, but somehow it did not recognized the hardware related files properly and complained about missing declarations. Adding the extra defines in the JSON solved it.
LikeLike
Hey Erich,
It’s been 8 years since I’ve left a comment, so now would be a good time.
Thanks for all the awesome write ups you do.
Do you know how to get IntelliSense to include multi-root folders in c_cpp_properties.json?
My workspace has several folders for libraries, who’s absolute paths exist outside of the main workspace folder (the folder that contains the .vscode folder). The lib folders are added to the workspace but the
“includePath”: [“${workspaceFolder}/**”]
line only searches the parent folder of the .vscode folder!
I’m looking to avoid hardcodes but searching the list of VSC vars didn’t reveal anything helpful.
LikeLiked by 1 person
welcome back as commenter :-).
I did check about how to enable IntelliSense for Multi-Root workspaces, and it seems to be a long outstanding issue (or request). I did not find any solution except many open tickets including this one: https://github.com/microsoft/vscode-cpptools/issues/2060
So I think it probably won’t be supported in the near future neither 😦
LikeLike
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 7 – FreeRTOS | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 8 – xPack C/C++ Managed Build Tools | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 9 – RTT | MCU on Eclipse
Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 10 – Assembly Stepping | MCU on Eclipse
Heads up and a quick fix… Many points in this series you use “+:” including on this page, but it appears you actually mean ++P
CTRL+P is the file finder
CTRL+SHIFT+P is the command finder
LikeLiked by 1 person
Thanks, I’ll have that changed.
LikeLike