# Customizing linker and redirecting the STDIO to a file

**URL:** https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217
**Category:** Usage
**Created:** [October 18, 2023, 3:03pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217 "2023-10-18T15:03:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 18, 2023, 3:03pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/1 "2023-10-18T15:03:33Z")

</div>

Hi,

I am using an ancient WindRiver diab compiler / linker. I managed to configure the toolchain so it properly finds the commands. My problem now is that the linker outputs the map file to the stdio, only. So, as simple as that, I thought adding a redirect to CMAKE\_C\_LINK\_EXECUTABLE would solve the matter

**set( CMAKE\_C\_LINK\_EXECUTABLE “${CMAKE\_C\_LINKER} \<CMAKE\_C\_LINK\_FLAGS\> \<LINK\_FLAGS\> -o \> .map” )**

This variant fails when running cmake during the toolchain check.

So I added it to the linker flags and reorder the assembly of the command line so the redirect is at the end. Not nice, but worth a try.

**${CMAKE\_C\_LINKER} -o \<CMAKE\_C\_LINK\_FLAGS\> \<LINK\_FLAGS\>**

The result is the linker complaining that “\> out.map” was assumed to be an input file which could not be found. In verbose mode, I copied the command line displayed to the shell prompt and there it worked well.

Any idea how I can redirect output of certain build steps to target specific text file?

Christoph

---

<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: [October 18, 2023, 3:29pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/2 "2023-10-18T15:29:56Z")

</div>

Have a look at the [CMAKE\_\<LANG\>\_LINKER\_LAUNCHER](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_LINKER_LAUNCHER.html) variable.

---

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 18, 2023, 4:35pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/3 "2023-10-18T16:35:16Z")

</div>

Marc,

thanks for fast response.

Documentation to the variable is not very comprehensible so I could work with it. Is it similar to **CMAKE\_C\_LINK\_EXECUTABLE**?

The part which confuses me is the fact that the resulting Makefile outputs a command line in verbose mode for which link fails. But when copy&paste this very line to the shell prompt, it works.

Any idea or further guidance where I can find more elaborated information about the variable you mentioned?

Christoph

---

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 18, 2023, 4:51pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/4 "2023-10-18T16:51:54Z")

</div>

A little bit more details, maybe towards people being familiar with cmake under the hood and experts to UNIX IO:

Linking is done by **link.txt**. This file contains a Make rule for the executable. This file is not included by a parent Makefile but it is passed as a parameter to cmake from **build.make**. It looks like

some\_ELF: **CMakeFiles/some\_ELF.dir/link.txt**  
@$(CMAKE\_COMMAND) -E cmake\_echo\_color “–switch=$(COLOR)” --green --bold --progress-dir=CMakeFiles --progress-num=$(CMAKE\_PROGRESS\_22) “Linking C executable some\_ELF”  
**$(CMAKE\_COMMAND) -E cmake\_link\_script CMakeFiles/some\_ELF.dir/link.txt** --verbose=$(VERBOSE)

This means, that any redirection is passed as a parameter to cmake, will cause confusions because redirection is a shell feature.

Has anyone an idea how to redirect stdout in a different manner than **\> file** on command line? Or is that what you meant with the **…\_LAUNCHER** variable?

Christoph

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [October 18, 2023, 8:05pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/5 "2023-10-18T20:05:44Z")

</div>

You could make a wrapper shell script of your own the linker. This script can solve the problem internally.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 18, 2023, 9:06pm UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/6 "2023-10-18T21:06:45Z")

</div>

Note that when CMake runs the linker, it does so directly, not via a shell. Therefore, you can’t use any shell redirection symbols as part of the command line. I’d recommend you follow @hsattler’s suggestion of writing your own wrapper script that handles the redirection internally.

---

<div class="post-metadata">

### Author: ![ChristophR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/258eb7/32.png) [@ChristophR](https://discourse.cmake.org/u/ChristophR)
#### Post date: [October 19, 2023, 9:42am UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/7 "2023-10-19T09:42:25Z")

</div>

Thanks, I now use  
set( CMAKE\_C\_LINK\_EXECUTABLE “/bin/sh -c '${CMAKE\_C\_LINKER} \<CMAKE\_C\_LINK\_FLAGS\> \<LINK\_FLAGS\> \<OBJECTS\> -o \<TARGET\> \> \<TARGET\>.map '” )

and it works.

Christoph

---

<div class="post-metadata">

### Author: ![wild.boy](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/fbc32d/32.png) [@wild.boy](https://discourse.cmake.org/u/wild.boy)
#### Post date: [March 21, 2025, 8:40am UTC](https://discourse.cmake.org/t/customizing-linker-and-redirecting-the-stdio-to-a-file/9217/8 "2025-03-21T08:40:39Z")

</div>

Could you please send your CMakeLists.txt script? There is a problem that the default default.dld script file is used when compiling, but the script is not set globally. What is the cause?
