Skip to main content
Explorer
June 11, 2025
Solved

Problems with C++ and STM32 extension in VS Code

  • June 11, 2025
  • 2 replies
  • 1108 views

I am having a hard time to set up a STM32 project in VS Code that works with all C++ features.

I set up my CMakeLists and enabled CXX language and add all my sources and so on.

When I keep my main.c and get my c++ functions with extern form a cpp file, the compiler does not find c++ functions like <cmath> that I include in my hpp headers.

I also tried setting up a project that I could open in VS Code and CubeIDE. In Cube IDE I renamed the main.c to main.cpp . And there in Cube IDE everything compiles fine.

In VS Code I get errors for the same project like and <cmath> is only found when I manually include the arm-none-eabi folder:

 undefined reference to `MX_SPI1_Init'",


What is the best way transform a CMake project from CubeMX to C++?

    This topic has been closed for replies.
    Best answer by JanusAudio

    Thanks a lot for the offer!

    I found the solution to my problem, but you might incorporate it into future updates of the STM Extension. When using STM32Cube clangd, there is a setting in settings.json (that is auto generated by the extension I assume):

    "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/13.3.1+st.8/bin/arm-none-eabi-gcc"

    This seems to be the reason for the error messages.

    When I change that option to ".../arm-none-eabi-g++" my project didn't display any error messages anymore and clangd recognizes the C++ libraries.

    And it seems like the STM32 extension changes that setting back to gcc each time I open my project... so would be nice to change that behavior.

    2 replies

    Technical Moderator
    June 16, 2025

    Hi @JanusAudio 

    If there is no confidential aspect n your project could you please share it by message we will help you transforming it to a C++ project. 

     

    JanusAudioAuthorAnswer
    Explorer
    June 16, 2025

    Thanks a lot for the offer!

    I found the solution to my problem, but you might incorporate it into future updates of the STM Extension. When using STM32Cube clangd, there is a setting in settings.json (that is auto generated by the extension I assume):

    "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/13.3.1+st.8/bin/arm-none-eabi-gcc"

    This seems to be the reason for the error messages.

    When I change that option to ".../arm-none-eabi-g++" my project didn't display any error messages anymore and clangd recognizes the C++ libraries.

    And it seems like the STM32 extension changes that setting back to gcc each time I open my project... so would be nice to change that behavior.

    Explorer
    June 19, 2025

    I was having this issue as well while working on a TouchGFX CMake project in VSCode, which uses C as well as C++ for TouchGFX. Lots of language errors and problems finding headers like assert.h. 
    My solution was to disable the STM32 clang extension and just use the C/C++ intellisense and that works fine.

    I did try the solution above but it will only solve some issues. I was able to fix all the problems by having both gcc and g++ for the query-driver setting.

     "stm32cube-ide-clangd.arguments": [
     "starm-clangd",
     "--compile-commands-dir=${workspaceFolder}/build/Debug",
     "--header-insertion=never",
     "--clang-tidy",
     "--clang-tidy-checks=-*,clang-diagnostic-undeclared-var-use",
     "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/13.3.1+st.8/bin/arm-none-eabi-g++.exe",
     "--query-driver=${env:CUBE_BUNDLE_PATH}/gnu-tools-for-stm32/13.3.1+st.8/bin/arm-none-eabi-gcc.exe"
     ],