cancel
Showing results for 
Search instead for 
Did you mean: 

How to specify separate compile flags for C and C++ using clangd extension for Visual Studio Code?

HTD
Senior III

I struggle all day with configuring clangd extension for Visual Studio Code to be able to provide all code intelligence feature for STM32 project that was created with STM32CubeIDE.

I mean - it works. Kind of. I provided a ridiculous number of include directories, symbols and such (mostly copied from STM32CubeIDE), but the problem is some flags should be set differently for C and C++ files.

It seems like STM32CubeIDE does that somehow.

With clangd I figured out I can create compile_flags.txt file and point the clang extension to use that file (--compile-commands-dir=.vscode).

So here is the beginning of my compiler_flags.txt file:

-mcpu=cortex-m7
-std=gnu11
-std=gnu++17
-D__GNUC__
-DDEBUG
-DCORE_CM7
-DUSE_HAL_DRIVER
-DSTM32H745xx
#-U__clang__
-U_WIN32
-ffunction-sections
-fdata-sections
-fno-exceptions
-fno-rtti
-fno-use-cxa-atexit
-Wall
-Wno-unknown-pragmas
--specs=nano.specs
-mfpu=fpv5-d16
-mfloat-abi=hard
-mthumb
-IC:\ST\STM32CubeIDE_1.12.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.200.202301161003\tools\lib\gcc\arm-none-eabi\10.3.1\include
-IC:\ST\STM32CubeIDE_1.12.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.200.202301161003\tools\lib\gcc\arm-none-eabi\10.3.1\include-fixed
-IC:\ST\STM32CubeIDE_1.12.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.200.202301161003\tools\arm-none-eabi\include\c++\10.3.1
-IC:\ST\STM32CubeIDE_1.12.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.200.202301161003\tools\arm-none-eabi\include\c++\10.3.1\arm-none-eabi\thumb\v7e-m+dp\hard
-IC:\ST\STM32CubeIDE_1.12.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.200.202301161003\tools\arm-none-eabi\include

However, this produce errors like this:


_legacyfs_online_stmicro_images_0693W00000bj5KOQAY.pngYou get the point, some flags should be provided for the C compiler, some for the C++ compiler. How to achieve that goal?

BTW, I tried to used Microsoft C++ extension first, but it failed miserably with code completion. It offered me symbols from the entire codebase, instead of symbols from the class I was editing.

For that matter clangd is amazingly good. Its code completions are better than those offered by STM32CubeIDE. The only problem are those spurious errors.

For now I don't intend to compile the code with VS Code, I'm just using it as an editor.

1 REPLY 1
HTD
Senior III

I found a partial solution:

.vscode/settings.json:

{
    "clangd.arguments": [
        "--compile-commands-dir=.vscode",
        "--enable-config"
    ],
    "clangd.onConfigChanged": "restart",
}

.clangd:

CompileFlags:
    Add:
        - "-mcpu=cortex-m7"
        - "-DDEBUG"
        - "-DCORE_CM7"
        - "-DUSE_HAL_DRIVER"
        - "-DSTM32H745xx"
        - "-D__GNUC__"
        - "-U_WIN32"
        - "-ffunction-sections"
        - "-fdata-sections"
        - "-mfpu=fpv5-d16"
        - "-mfloat-abi=hard"
        - "-mthumb"
        - "-Wall"
        - "-Wno-unknown-pragmas"
        - "--specs=nano.specs"
---
If:
    PathMatch: [.*\.c, .*\.h]
CompileFlags:
    Add:
        - "-std=gnu11"
        - "-I/source/embedded/stm32-gnu-tools/arm-none-eabi/include"
        - "-I/source/embedded/stm32-gnu-tools/lib/gcc/arm-none-eabi/10.3.1/include"
        - "-I/source/embedded/stm32-gnu-tools/lib/gcc/arm-none-eabi/10.3.1/include-fixed"
---
If:
    PathExclude: [.*\.c, .*\.h]
CompileFlags:
    Add:
        - "-std=gnu++17"
        - "-fno-exceptions"
        - "-fno-rtti"
        - "-fno-use-cxa-atexit"
        - "-I/source/embedded/stm32-gnu-tools/arm-none-eabi/include"
        - "-I/source/embedded/stm32-gnu-tools/arm-none-eabi/include/c++/10.3.1"
        - "-I/source/embedded/stm32-gnu-tools/arm-none-eabi/include/c++/10.3.1/arm-none-eabi/thumb/v7e-m+dp/hard"

File .vscode/compile_flags.txt contains only my project include paths.

Most of the files are processed without errors, however, I still get something like this:


_legacyfs_online_stmicro_images_0693W00000bj5OkQAI.png