Skip to main content
fz_gabriel
Associate
February 13, 2025
Question

Feature request: Add custom compiler flags to CMake generated project

  • February 13, 2025
  • 2 replies
  • 1233 views

The new CMake generation is great! It's very tastefully done (toochain file, presets, simple CMakeLists.txt, etc).

My request: I want to add some additional compiler flags (like `-Wconversion`) to my project.

In the top CMakeLists.txt I did:

```cmake

target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
"-Wconversion"
)

```

The problem is that it applies to the HAL as well, generating an endless stream of errors.

I could create a new cmake target for my own functions, but I wish the generated CMake template would support this out of the box.

 

 

Regards,

Gabriel

2 replies

Technical Moderator
February 13, 2025

Hello @fz_gabriel 

Thank you for your contribution!

Have you tried to add the flags in gcc-arm-none-eabi.cmake file under the MCU specific flags section?

BR,

Souhaib   

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
fz_gabriel
Associate
February 13, 2025

Hi Souhaib,

 

Thanks for your answer. 

I would prefer not to touch the toolchain file.

In my opinion, the toolchain file should be reserved for making the toolchain work (i.e. providing the linker flags), not for project-specific development flags like `-Wshadow` or `-Wpedantic`.

In an ideal world, I could add development flags to everything except the ST-HAL and CMSIS Driver.

Does that make sense? 

 

Best,

Gabriel

 

Pavel A.
Super User
February 13, 2025

Could this be done as the post-generation custom action?

 

fz_gabriel
Associate
February 14, 2025

> Could this be done as the post-generation custom action?

 

Of course. I opened a feature request because, in my opinion, the generated CMake should separate ST-HAL from the rest of the program so I can use stricter debug flags on my code and not on ST's code.

 

Technical Moderator
February 18, 2025

Hello @fz_gabriel 

Actually, this request has been discussed in this thread, and an improvement of the CMake structure will be implemented in the coming CubeMX releases.

BR,

Souhaib

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.