DEV Community

Cover image for CMake on STM32 | Episode 3: handle warnings in 3rd party files

CMake on STM32 | Episode 3: handle warnings in 3rd party files

pgradot profile image Pierre Gradot Updated on ・3 min read

Something funny happens sometimes with 3rd party files: they generate warnings when compiled with your project settings. If you have quality indicators about the build health (automatically updated by Jenkins maybe), this is quite annoying. If you compile with an option such as -Werror to treat warnings as errors, this is a blocking issue.

In this article, we will add compiler options that will raise warnings in ST's generated files and we will ask CMake to suppress these warnings but only for these files.

This article is part of a series about CMake and STM32.

Add compilers options

In episode 1, we simply used the compiler options from ST's generated Makefiles. The only option to control GCC warnings is -Wall. If this is not clear for you yet, I will say it very clearly : -Wall does not enable all warnings and compiling with -Wall is not enough. I suggest that you use at least -Wall -Wextra. This GitHub repository has lists of options to enable warnings. Have a look at the options they suggest for C with GCC 9...

For the example here, let's add -Wextra -pedantic. Simply modify the call to target_compile_options() in CMakeLists.txt:

target_compile_options(${EXECUTABLE} PRIVATE




When you recompile the project, you will see warnings that you didn't get before:

[ 48%] Building C object CMakeFiles/nucleo-f413zh.out.dir/BSP/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c.obj
D:\cmake_stm32\BSP\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c: In function 'HAL_PWR_EnterSLEEPMode':
D:\cmake_stm32\BSP\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c:365:38: warning: unused parameter 'Regulator' [-Wunused-parameter]
  365 | void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
      |                             ~~~~~~~~~^~~~~~~~~

Change compiler options for a specific file

A this point we have a problem:

  1. It is out of the question that we modify generated / 3rd party files.
  2. There is no way that we remove -Wextra (that adds -Wunused-parameter).

We may add -Wno-unused-parameter to target_compile_options() to counter -Wunused-parameter. It will suppress the warnings on ST's generated file but also for our own files. This is not what we want because it would be useful to be warned if a function we wrote doesn't use one of its parameters.

The solution is to add -Wno-unused-parameter only to these generated files. Our own code will then still compile with -Wunused-parameter.

The CMake function to do this is set_source_files_properties().

We modify our CMakeLists.txt again to add this:

            COMPILE_FLAGS -Wno-unused-parameter)

The project builds again with no warning! Great!


It is always to good to configure the compiler with many options to be warned about suspicious code. In C and C++, warnings are often potential bugs / not portable constructs / performance issues / etc. We should rely on the compiler to tell us about such code.

If adding options raise warnings in files that we don't own and can't modify, we can use CMake's set_source_files_properties() function to apply specific options to these files to suppress the warnings.

Discussion (0)

Editor guide