How to specify separate compile flags for C and C++ using clangd extension for Visual Studio Code?
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\includeHowever, this produce errors like this:
You 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.