Visual Studio Code for C/C++ with ARM Cortex-M: Part 6 – IntelliSense

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:

Code Completion in Visual Studio

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):

Peek Definition

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

Code Completion

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:

Sneak Preview

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

12 thoughts on “Visual Studio Code for C/C++ with ARM Cortex-M: Part 6 – IntelliSense

  1. Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 2 | MCU on Eclipse

  2. Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 3 | MCU on Eclipse

  3. Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 4 | MCU on Eclipse

  4. Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 5 | MCU on Eclipse

  5. Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 1 | MCU on Eclipse

  6. 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…

    Liked 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.

      Like

  7. 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.

    Liked by 1 person

  8. Pingback: Visual Studio Code for C/C++ with ARM Cortex-M: Part 7 – FreeRTOS | MCU on Eclipse

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.