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

**URL:** https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946
**Category:** Usage
**Created:** [June 5, 2024, 8:45am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946 "2024-06-05T08:45:24Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AntoineN](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AntoineN](https://discourse.cmake.org/u/AntoineN)
#### Post date: [June 5, 2024, 8:45am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/1 "2024-06-05T08:45:24Z")

</div>

Hello,

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

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:

```auto
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

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [June 5, 2024, 1:41pm UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/2 "2024-06-05T13:41:53Z")

</div>

Try to set the [SYSTEM](https://cmake.org/cmake/help/latest/prop_tgt/SYSTEM.html) property on your `stm32cubemx` target:

```cmake
set_property(TARGET stm32cubemx PROPERTY SYSTEM ON)

```

---

<div class="post-metadata">

### Author: ![AntoineN](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AntoineN](https://discourse.cmake.org/u/AntoineN)
#### Post date: [June 5, 2024, 4:04pm UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/3 "2024-06-05T16:04:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 8, 2024, 2:32am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/4 "2024-06-08T02:32:11Z")

</div>

How are you defining the `stm32cubemx` target?

---

<div class="post-metadata">

### Author: ![AntoineN](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AntoineN](https://discourse.cmake.org/u/AntoineN)
#### Post date: [June 10, 2024, 9:19am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/5 "2024-06-10T09:19:55Z")

</div>

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

```auto
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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 10, 2024, 11:54am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/6 "2024-06-10T11:54:05Z")

</div>

> [@AntoineN](#):
>
> ```auto
> target_sources(stm32cubemx INTERFACE
> ../../Core/Src/main.c
> 
> #list of all the stm32 source files
>     
> )
> 
> ```

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:

> [@AntoineN](#):
>
> still try to compile the stm32cubemx with the compile option of the libPWM

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.

---

<div class="post-metadata">

### Author: ![AntoineN](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AntoineN](https://discourse.cmake.org/u/AntoineN)
#### Post date: [June 11, 2024, 10:37am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/7 "2024-06-11T10:37:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 11, 2024, 1:41pm UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/9 "2024-06-11T13:41:10Z")

</div>

> [@AntoineN](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![AntoineN](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AntoineN](https://discourse.cmake.org/u/AntoineN)
#### Post date: [June 18, 2024, 10:19am UTC](https://discourse.cmake.org/t/linking-to-library-created-as-interface-propagate-all-the-compile-option-and-create-error/10946/10 "2024-06-18T10:19:29Z")

</div>

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
