# arm gcc newlib retargetable locking fails because of command line order

**URL:** https://discourse.cmake.org/t/arm-gcc-newlib-retargetable-locking-fails-because-of-command-line-order/9622
**Category:** Code
**Created:** [December 13, 2023, 11:28am UTC](https://discourse.cmake.org/t/arm-gcc-newlib-retargetable-locking-fails-because-of-command-line-order/9622 "2023-12-13T11:28:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DavidRegan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/d07c76/32.png) [@DavidRegan](https://discourse.cmake.org/u/DavidRegan)
#### Post date: [December 13, 2023, 11:28am UTC](https://discourse.cmake.org/t/arm-gcc-newlib-retargetable-locking-fails-because-of-command-line-order/9622/1 "2023-12-13T11:28:35Z")

</div>

Link step fails with multiple definition error because cmake has created a link command of the form  
\<CMAKE\_C\_COMPILER\> $FLAGS $LINK\_FLAGS @RSP\_FILE -o $TARGET\_FILE.

The $LINK\_FLAGS contain -lc -lm. The problem is that this causes the dummy retargetable locks from newlib to be selected and then the objects list (RSP\_FILE) causing multiple definitions.

I need the command to be of the form  
\<CMAKE\_C\_COMPILER\> $FLAGS @RSP\_FILE $LINK\_FLAGS .-o $TARGET\_FILE

I used message(STATUS “CMAKE\_C\_LINK\_EXECUTABLE ${CMAKE\_C\_LINK\_EXECUTABLE}”) to show the contents and the result was  
CMAKE\_C\_LINK\_EXECUTABLE \<CMAKE\_C\_COMPILER\> \<CMAKE\_C\_LINK\_FLAGS\> \<LINK\_FLAGS\> -o \<LINK\_LIBRARIES\>

This is obviously being overridden during the generation process

---

<div class="post-metadata">

### Author: ![DavidRegan](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/d07c76/32.png) [@DavidRegan](https://discourse.cmake.org/u/DavidRegan)
#### Post date: [December 13, 2023, 5:22pm UTC](https://discourse.cmake.org/t/arm-gcc-newlib-retargetable-locking-fails-because-of-command-line-order/9622/2 "2023-12-13T17:22:44Z")

</div>

To answer my own problem  
I added  
set(CMAKE\_C\_LINK\_EXECUTABLE  
“\<CMAKE\_C\_COMPILER\> \<CMAKE\_C\_LINK\_FLAGS\> OBJECTS \<LINK\_FLAGS\> -o \<LINK\_LIBRARIES\>”)  
BEFORE the project statement

p.s. OBJECTS should be in \<\> but must be some kind of formatting going on

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [December 13, 2023, 10:14pm UTC](https://discourse.cmake.org/t/arm-gcc-newlib-retargetable-locking-fails-because-of-command-line-order/9622/3 "2023-12-13T22:14:08Z")

</div>

Consider using `target_link_libraries(myexe PRIVATE -lc -lm)`.  
Then CMake handles them as libraries like any other, and not just as some non-library linker flag.

P.S. use preformatted text or a code block for posting code fragments. Either via editor symbols or markdown syntax.
