# recplacing linker script option

**URL:** <https://discourse.cmake.org/t/recplacing-linker-script-option/6926>\
**Category:** Code\
**Created:** [November 22, 2022, 10:59am UTC](https://discourse.cmake.org/t/recplacing-linker-script-option/6926 "2022-11-22T10:59:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JojoS62](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jojos62/32/1890_2.png) [@JojoS62](https://discourse.cmake.org/u/JojoS62)\
**Post date:** [November 22, 2022, 10:59am UTC](https://discourse.cmake.org/t/recplacing-linker-script-option/6926/1 "2022-11-22T10:59:26Z")

</div>

I have an arm-eabi-none environment (mbed-os) and want to add a custom linker script option to the build system. The current system uses a fixed linker script, assigned to the used target mcu/board.

The original script is set by

```cmake
    add_custom_target(mbed-linker-script DEPENDS ${LINKER_SCRIPT_PATH} VERBATIM)
    foreach(TARGET ${mbed_baremetal_target} ${mbed_os_target})
        add_dependencies(${TARGET} mbed-linker-script)

        # Add linker flags to the MCU target to pick up the preprocessed linker script
        target_link_options(${TARGET}
            INTERFACE
                "-T" "${LINKER_SCRIPT_PATH}"
        )
    endforeach()

```

Now I want to override this option in a later, optional step.

```cmake
        remove_flag_from_target(${TARGET} "-T" "${LINKER_SCRIPT_PATH}")

        # Add linker flags to the MCU target to pick up the preprocessed linker script
        target_link_options(${TARGET}
            INTERFACE
                "-T" "${CUSTOM_LINKER_SCRIPT_PATH}"
        )

```

The remove flag macro is from Stackoverflow, it reads the options into a list and removes the item if found::

```cmake
macro(remove_flag_from_target _target _flag)
    get_target_property(_target_cxx_flags ${_target} INTERFACE_LINK_OPTIONS)
    if(_target_cxx_flags)
        list(REMOVE_ITEM _target_cxx_flags ${_flag})
        set_target_properties(${_target} PROPERTIES INTERFACE_LINK_OPTIONS "${_target_cxx_flags}")
    endif()
endmacro()

```

But this does not work, I have tried now many versions of writing the flags, it will not remove -T and the script name. What is missing?

after executing this, the remaining INTERFACE\_LINK\_OPTIONS are:

```auto
[cmake] INTERFACE_LINK_OPTIONS in mbed-os: -Wl,--gc-sections-Wl,--wrap,main-Wl,--wrap,_malloc_r-Wl,--wrap,_free_r-Wl,--wrap,_realloc_r-Wl,--wrap,_memalign_r-Wl,--wrap,_calloc_r-Wl,--wrap,exit-Wl,--wrap,atexit-Wl,-n/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/mbed-disco-f769ni.link_script.ld-T/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/app_Hello/app_hello.link_spript.ld

```

only the first flag id ‘-T’ is removed.

---

<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:** [November 22, 2022, 5:41pm UTC](https://discourse.cmake.org/t/recplacing-linker-script-option/6926/2 "2022-11-22T17:41:34Z")

</div>

That `remove_flag_from_target` function only removes the first argument you pass. I think you should experiment with using that to remove its arguments from a list and then move it to doing the target property stuff.

---

<div class="post-metadata">

**Author:** ![JojoS62](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jojos62/32/1890_2.png) [@JojoS62](https://discourse.cmake.org/u/JojoS62)\
**Post date:** [November 22, 2022, 7:26pm UTC](https://discourse.cmake.org/t/recplacing-linker-script-option/6926/3 "2022-11-22T19:26:03Z")

</div>

thanks, good idea.  
It did not work, but I’ve printed the name of the LINKER\_SCRIPT\_PATH that I want to remove and it was empty at that point. So I have a clue where to find the problem.

```cmake
        message("LINKDER_SCRIPT_PATH: " ${LINKER_SCRIPT_PATH})
        remove_flag_from_target(${TARGET} "-T")
        remove_flag_from_target(${TARGET} "${LINKER_SCRIPT_PATH}")

```

```auto
[cmake] LINKDER_SCRIPT_PATH: /home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/.link_script.ld

```

does not match

```auto
[cmake] INTERFACE_LINK_OPTIONS in mbed-os: -Wl,--gc-sections-Wl,--wrap,main-Wl,--wrap,_malloc_r-Wl,--wrap,_free_r-Wl,--wrap,_realloc_r-Wl,--wrap,_memalign_r-Wl,--wrap,_calloc_r-Wl,--wrap,exit-Wl,--wrap,atexit-Wl,-n/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/mbed-disco-f769ni.link_script.ld-T/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/app_Hello/app_hello.link_spript.ld

```

still learning CMake.
