Linking to library created as INTERFACE propagate all the compile option and create error


I’m working on a project using a lib create as an INTERFACE (stm32cubemx lib, generate by STM32CubeMX, so I cannot change it :frowning: )

I have a lib, libPWM that have to link to the stm32cubemx lib. In my lib, I like to reduce possible error so I add -Wconversion -Werror as compile option in the libPWM CMakeLists.txt

SO I have in the libPWM CMakeLists.txt file:

target_link_libraries( libPWM

target_compile_option( libPWM PRIVATE

My problem is that, when compiling the libPWM, as the stm32cubemx lib is define as an INTERFACE, it is regenerate using its own compile option PLUS the libPWM compile option. As the ST code is not perfect, the stm32cubemx lib do not compile.

SO, how to link with an interface lib, without propagating the compile option of the lib linking to it?

Thank you for your help.

Try to set the SYSTEM property on your stm32cubemx target:

set_property(TARGET stm32cubemx PROPERTY SYSTEM ON)


I tried it but it do not worked. When trying to compile the libPWM, it still try to compile the stm32cubemx with the compile option of the libPWM.

The difference is only on the include directory that are add with -isystem instead of -I

How are you defining the stm32cubemx target?

Here is a copy of the stm32cubemx CMakeLists.txt file:

cmake_minimum_required(VERSION 3.22)

add_library(stm32cubemx INTERFACE)

# Enable CMake support for ASM and C languages
enable_language(C ASM)

target_compile_definitions(stm32cubemx INTERFACE 

target_include_directories(stm32cubemx INTERFACE

target_sources(stm32cubemx INTERFACE

    #list of all the stm32 source files

target_link_directories(stm32cubemx INTERFACE

target_link_libraries(stm32cubemx INTERFACE

# Validate that STM32CubeMX code is compatible with C standard
    message(ERROR "Generated code requires C11 or higher")

As I said, this file is auto generated by STM32CubeMX. My first intention was to keep it with no modification. But from what I understand (I’m starting with CMake, so I’m not an expert using it), it will not be possible to use it like I want (no modification of the compile options when linking to another lib among other things) as long as it is defined as an interface.

I think I will change the lib to be a non interface and modify the file generated. I do not understand why STM32CubeMX is generated an interface. But I will have to de with it

This says “add these sources to the source list of anything that links to stm32cubemx”. Is this what you intend? Is it these files you’re seeing be compiled when you say:

If so, these files are, at the time, part of libPWM and, as such, they will get libPWM’s flags. If you want them compile separately, make stm32cubemx a non-INTERFACE library. Making it an OBJECT library is probably closest to the desired behavior, but I’m not sure.


Thank you for your answer. It confirm what I was starting to understand from the INTERFACE library.

Il will modify the generated stm32cubemx CMakeLists.txt file to make the lib a non interface library.

What I do not understand is what is the interest to use INTERFACE lib instead of static lib.


When a target represents some collection of requirements that doesn’t involve an actual library file. For example, a collection of flags or includes.

Ok thanks.
For me, it is not the case in the STM32CubeMX project, but I understand the normal goal of interface lib. And the fact that it cannot do what I want. I will try to modify it.
Thank you