Customizing linker and redirecting the STDIO to a file

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

Have a look at the CMAKE_<LANG>_LINKER_LAUNCHER variable.

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

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

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

1 Like

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.

1 Like

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

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?