cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE: JSON Compilation Database Generator does not create correct compile_commands.json

Gaweringo
Associate II

I am looking for a way to use a different editor for a STM32CubeIDE project. For the editor to show correct errors and provide code-completion, I would need a compile_commands.json file, which the clangd language server can use.

I found this post which describes how to generate this file using an external program called compiledb. But I also found a setting in STM32CubeIDE under Project -> Properties -> C/C++ Build -> JSON Compilation Database Generator, which can be enabled to have STM32CubeIDE generate a compile_commands.json file when building automatically, which would be exactly what I need.

Gaweringo_0-1744723997926.png

But the created compile_commands.json file seems to be not correct, specifically the relative paths used for the includes are wrong.

An exemplary entry in the compile_commands.json file looks like this:

{
"directory": "/full/path/to/project/dir",
"command": "arm-none-eabi-gcc \"/full/path/to/project/dir/Drivers/BSP/STM32H7xx_Nucleo/stm32h7xx_nucleo.c\" -mcpu\u003dcortex-m7 -std\u003dgnu11 -g3 -DDEBUG -DUSE_PWR_DIRECT_SMPS_SUPPLY -DUSE_NUCLEO_64 -DUSE_HAL_DRIVER -DSTM32H7A3xxQ -c -I../Inc -I../Drivers/STM32H7xx_HAL_Driver/Inc -I../Drivers/STM32H7xx_HAL_Driver/Inc/Legacy -I../Drivers/BSP/STM32H7xx_Nucleo -I../Drivers/CMSIS/Device/ST/STM32H7xx/Include -I../Drivers/CMSIS/Include -I\"..\\dependencies\\CMSIS-DSP\\Include\" -I\"..\\dependencies\\CMSIS-DSP\\PrivateInclude\" -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage --specs\u003dnano.specs -mfpu\u003dfpv5-d16 -mfloat-abi\u003dhard -mthumb -o \"/full/path/to/project/dir/Debug/Drivers/BSP/STM32H7xx_Nucleo/stm32h7xx_nucleo.o\"",
"file": "/full/path/to/project/dir/Drivers/BSP/STM32H7xx_Nucleo/stm32h7xx_nucleo.c"
},

The relevant part is, that the "directory" is the path to the top level project directory, yet all the -I include paths in "command", are relative to the ./Debug / output directory in which the compile_commands.json file is located.
Thus, clangd cannot find any of the #include "xxx.h" files.

This is either a problem with the generation of the compile_commands.json file, or it is my problem, in case I simply did not use it correctly / there is a setting to change this behavior. If it's the second, I would appreciate some guidance on how to correct this.

 

Another note: If I manually add /Debug to all the "directory" paths, the includes work, except for all the standard C includes like math.h, string.h, ... which are also used in the HAL files. This would also be something which I would be glad if someone could help with.

3 REPLIES 3
CCvanE
Associate

Hi, 

Did you ever find a solution to this? I have exactly the same problem.

 

Sadly not yet, although I haven't tried that hard after making this post.
For me, it looks like, it's something that STM has to fix.

I have the same issue.
I need compile_commands.json to be able to use clang tidy.
Setting up the project to build with clang is possible, but I want to be able to do it with GCC projects too.
I was looking for other ways to create a compilation database from the build artifacts, but that was too much of a hassle. Then I discovered the JSON Compilation Database Generator option. I was happy, but then clang tidy was reporting it could not find include files e.g.:

usb_host.c:23:10: error: 'usb_host.h' file not found [clang-diagnostic-error]

For now my workaround is to replace "directory" value with "." and run clang tidy from inside the build folder

Additionally I want to be able to save the setting in git once it works. But unfortunately STM32CubeIDE saves the setting inside the workspace. So every developer has to enable the setting for every project manually and headless build doesn't work. https://community.st.com/t5/stm32cubeide-mcus/headless-linux-build-need-compile-commands-json-for-pclintplus/m-p/882423

 

Update: Here is a python script to generate compile_commands.jsonhttps://stackoverflow.com/a/79933800/15307950

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.