Futrime
Futrime

Reputation: 21

Why is VSCode keep warning me about include errors?

When I'm programming on my STM32 project, VSCode constantly underlines the code #include "main.h" with the reason belows:

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit. cannot open source file "stm32f1xx.h" (dependency of "main.h")

However, I double-checked my c_cpp_properties.json, finding nothing wrong with it. Meanwhile, VSCode has given me the quick fix method, adding ${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32F1xx/Include to includePath parameter, which didn't work either.

Belows are my configurations.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "USE_HAL_DRIVER",
                "STM32F103xE"
            ],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "compilerPath": "C:\\Program Files (x86)\\GNU Arm Embedded Toolchain\\10 2021.07\\bin\\arm-none-eabi-gcc.exe",
            "intelliSenseMode": "gcc-arm"
        },
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "USE_HAL_DRIVER",
                "STM32F103xE"
            ],
            "cStandard": "c17",
            "cppStandard": "c++17",
            "compilerPath": "/opt/ARM/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",
            "intelliSenseMode": "gcc-arm"
        }
    ],
    "version": 4
}

And the required file /Drivers/CMSIS/Device/ST/STM32F1xx/Include/stm32f1xx.h does exist and the compilation with make can be done correctly too.

I've check the files multiple times but didn't find any problem, like belows:

enter image description here

Upvotes: 1

Views: 2101

Answers (1)

lblenner
lblenner

Reputation: 452

You have to tell vscode where to find all the include files needed.
When you add an include folder to the includePath, the included headers inside the folder might include other header from elsewhere. This might explain why you still get the error after adding a single path to the includePath.

Project generated using CubeMX typically have multiple include folders.

Your configuration file should look something like that

"configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/Core/Inc/**",
                "${workspaceFolder}/Drivers/STM32F0xx_HAL_Driver/Inc/**",
                "${workspaceFolder}/Drivers/STM32F0xx_HAL_Driver/Inc/Legacy/**",
                "${workspaceFolder}/Drivers/CMSIS/Include/**",
                "${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32F0xx/Include/**"
            ],
            "defines": ["STM32F031x6","USE_HAL_DRIVER"],
            "compilerPath": "path_to_compiler",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],

To find the required folders, you have different to do it :

Using the Makefile

If you have a working Makefile, it probably contains a place where it defines the used header files, for example :

C_INCLUDES =  \
-IDrivers/STM32F0xx_HAL_Driver/Inc \
-IDrivers/STM32F0xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F0xx/Include \
-IDrivers/CMSIS/Include

Using Vscode intellisense

If there is a missing header, Vscode will show the following error

cannot open source file "HEADER2.h" (dependency of "HEADER1.h")

This means that HEADER2.h is included inside HEADER1.h but there is no HEADER2.h in the includePath.

So you should find the folder containing HEADER2.h and add it.
To do so, you can use find . -name "HEADER2.h" from the root of the project.

Upvotes: 0

Related Questions