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

Hello,

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
    PRIVATE
    stm32cubemx
)

target_compile_option( libPWM PRIVATE
    -Wconversion
    -Werror
)

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.
Regards
Antoine

Try to set the SYSTEM property on your stm32cubemx target:

set_property(TARGET stm32cubemx PROPERTY SYSTEM ON)

Hi,

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)

project(stm32cubemx)
add_library(stm32cubemx INTERFACE)

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

target_compile_definitions(stm32cubemx INTERFACE 
	USE_HAL_DRIVER 
	STM32F030x6
    $<$<CONFIG:Debug>:DEBUG>
)

target_include_directories(stm32cubemx INTERFACE
    ../../Core/Inc
    ../../Drivers/STM32F0xx_HAL_Driver/Inc
    ../../Drivers/STM32F0xx_HAL_Driver/Inc/Legacy
    ../../Drivers/CMSIS/Device/ST/STM32F0xx/Include
    ../../Drivers/CMSIS/Include
)

target_sources(stm32cubemx INTERFACE
    ../../Core/Src/main.c

    #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
if(CMAKE_C_STANDARD LESS 11)
    message(ERROR "Generated code requires C11 or higher")
endif()

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.

Hello,

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.

Antoine

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