# CMake Link.txt generated during CMake configuration does not contain any link flags or source files when using riscv toolchain

**URL:** https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304
**Category:** Usage
**Tags:** os:linux
**Created:** [July 2, 2025, 7:56am UTC](https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304 "2025-07-02T07:56:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Atul\_Joshi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/atul_joshi/32/5644_2.png) [@Atul\_Joshi](https://discourse.cmake.org/u/Atul_Joshi)
#### Post date: [July 2, 2025, 7:56am UTC](https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304/1 "2025-07-02T07:56:29Z")

</div>

I have 2 source files, one is C (cstart.c) and another is assembly (startup.S) using which i am trying to make an riscv executable.

Here is how my CMakeLists.txt Looks Like

=========================================================================  
cmake\_minimum\_required(VERSION 3.22.1)  
set(CMAKE\_TRY\_COMPILE\_TARGET\_TYPE “STATIC\_LIBRARY”)  
set(CMAKE\_TOOLCHAIN\_FILE “/workspaces/risc\_v/riscv\_toolchain.cmake”)  
project(riscv\_training\_lab C ASM)

message(STATUS “CMAKE\_SYSTEM\_NAME: ${CMAKE\_SYSTEM\_NAME}”)  
message(STATUS “CMAKE\_CROSSCOMPILING: ${CMAKE\_CROSSCOMPILING}”)

add\_custom\_command(  
OUTPUT ${CMAKE\_CURRENT\_BINARY\_DIR}/cstart.o  
COMMAND ${CMAKE\_ASM\_COMPILER} -c -g -O0 -ffreestanding -march=rv64i -mabi=lp64 -mcmodel=medany -o ${CMAKE\_CURRENT\_BINARY\_DIR}/cstart.o ${CMAKE\_CURRENT\_SOURCE\_DIR}/cstart.c  
DEPENDS ${CMAKE\_CURRENT\_SOURCE\_DIR}/cstart.c  
OUTPUT ${CMAKE\_CURRENT\_BINARY\_DIR}/startup.o  
COMMAND ${CMAKE\_ASM\_COMPILER} -c -g -O0 -ffreestanding -march=rv64i -mabi=lp64 -mcmodel=medany -o ${CMAKE\_CURRENT\_BINARY\_DIR}/startup.o ${CMAKE\_CURRENT\_SOURCE\_DIR}/startup.S  
DEPENDS ${CMAKE\_CURRENT\_SOURCE\_DIR}/startup.S

)

SET(OBJS  
${CMAKE\_CURRENT\_BINARY\_DIR}/cstart.o  
${CMAKE\_CURRENT\_BINARY\_DIR}/startup.o  
)

add\_executable(data\_stack ${OBJS})  
set\_target\_properties(data\_stack PROPERTIES LINKER\_LANGUAGE C)

=========================================================================

Here is how my toolchain file, which i am referring in above mentioned CMakeLists.txt looks like

========================================================================  
set(CMAKE\_SYSTEM\_NAME Generic)

set(CMAKE\_SYSTEM\_PROCESSOR riscv64)

set(RISCV\_TOOLCHAIN\_ROOT “/opt/riscv/freestanding/multilib”)

set(CMAKE\_C\_COMPILER\_TARGET riscv64-unknown-elf)

set(CMAKE\_CXX\_COMPILER\_TARGET riscv64-unknown-elf)

set(CMAKE\_C\_COMPILER ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-gcc)

set(CMAKE\_ASM\_COMPILER ${CMAKE\_C\_COMPILER})

set(CMAKE\_CXX\_COMPILER ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-g++)

set(CMAKE\_STRIP ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-strip)

set(CMAKE\_AR ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-gcc-ar)

set(CMAKE\_RANLIB ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-ranlib)

set(CMAKE\_ADDR2LINE ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-addr2line)

set(CMAKE\_NM ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-nm)

set(CMAKE\_LINKER ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-ld)

set(CMAKE\_OBJCOPY ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-objcopy)

set(CMAKE\_OBJDUMP ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-objdump)

set(CMAKE\_READELF ${RISCV\_TOOLCHAIN\_ROOT}/bin/riscv64-unknown-elf-readelf)

set(CMAKE\_LINKER ${CMAKE\_C\_COMPILER})

set(CMAKE\_C\_LINK\_EXECUTABLE ${CMAKE\_C\_COMPILER})

set(CMAKE\_CROSSCOMPILING TRUE)

========================================================================

When i run cmake command, the CMake link.txt file so generated doe snot show any source files. Here is how the contents of link.txt looks like

=================================================  
/opt/riscv/freestanding/multilib/bin/riscv64-unknown-elf-gcc

=================================================

I don’t see this issue when i dont use any toolchain. So if i use following CMakeLists.txt, i see link,txt showing source files

=========================================================================  
cmake\_minimum\_required(VERSION 3.22.1)

project(riscv\_training\_lab C ASM)

add\_custom\_command(  
OUTPUT ${CMAKE\_CURRENT\_BINARY\_DIR}/cstart.o  
COMMAND ${CMAKE\_ASM\_COMPILER} -c -g -O0 -ffreestanding -march=rv64i -mabi=lp64 -mcmodel=medany -o ${CMAKE\_CURRENT\_BINARY\_DIR}/cstart.o ${CMAKE\_CURRENT\_SOURCE\_DIR}/cstart.c  
DEPENDS ${CMAKE\_CURRENT\_SOURCE\_DIR}/cstart.c  
OUTPUT ${CMAKE\_CURRENT\_BINARY\_DIR}/startup.o  
COMMAND ${CMAKE\_ASM\_COMPILER} -c -g -O0 -ffreestanding -march=rv64i -mabi=lp64 -mcmodel=medany -o ${CMAKE\_CURRENT\_BINARY\_DIR}/startup.o ${CMAKE\_CURRENT\_SOURCE\_DIR}/startup.S  
DEPENDS ${CMAKE\_CURRENT\_SOURCE\_DIR}/startup.S  
)

SET(OBJS  
${CMAKE\_CURRENT\_BINARY\_DIR}/cstart.o  
${CMAKE\_CURRENT\_BINARY\_DIR}/startup.o  
)

add\_executable(data\_stack ${OBJS})  
set\_target\_properties(data\_stack PROPERTIES LINKER\_LANGUAGE C)

=========================================================================

Now the contents of link.txt shown all input and output files

=================================================  
/usr/bin/cc cstart.o startup.o -o data\_stack

=================================================

Can you please suggest what wrong is happening when i am using riscv toolchain

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [July 2, 2025, 8:10am UTC](https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304/2 "2025-07-02T08:10:12Z")

</div>

> [@Atul\_Joshi](#):
>
> CMAKE\_C\_LINK\_EXECUTABLE

Does not do what you think it does - it’s a rule how to build link command line

---

<div class="post-metadata">

### Author: ![Atul\_Joshi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/atul_joshi/32/5644_2.png) [@Atul\_Joshi](https://discourse.cmake.org/u/Atul_Joshi)
#### Post date: [July 2, 2025, 8:54am UTC](https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304/3 "2025-07-02T08:54:17Z")

</div>

> [@Atul\_Joshi](#):
>
> CMAKE\_C\_LINK\_EXECUTABLE

This is what i see for CMAKE\_C\_LINK\_EXECUTABLE,

======================================================================  
The value of `CMAKE_C_LINK_EXECUTABLE` typically includes placeholders that CMake replaces with specific values during generation:

- `<CMAKE_C_COMPILER>`: The C compiler being used.
- `<CMAKE_C_LINK_FLAGS>`: Linker flags automatically determined by CMake based on the build type and target properties.
- `<LINK_FLAGS>`: Linker flags specified explicitly by the user (e.g., through `target_link_options`).
- `<OBJECTS>`: The object files to be linked.
- `<TARGET>`: The name of the executable target.
- Other placeholders for specific options or paths.

Usage and Modification:

While `CMAKE_C_LINK_EXECUTABLE` can be modified, it’s generally best to avoid directly setting this variable unless absolutely necessary for highly specialized scenarios like custom toolchain integration or specific build system modifications.

Instead of directly manipulating `CMAKE_C_LINK_EXECUTABLE`, it is recommended to use standard CMake commands and properties for controlling the linking process, such as:

- `target_link_libraries()`: To link executables with libraries.
- `target_link_options()`: To add specific linker flags to a target.
- `CMAKE_C_FLAGS` and `CMAKE_C_FLAGS_<BUILD_TYPE>`: To add compiler/linker flags globally or for specific build types.
- Toolchain files: For cross-compilation environments, toolchain files are the appropriate place to define or override tool-specific variables like `CMAKE_C_LINK_EXECUTABLE`.  
=========================================================================

are you suggesting that i should get rid of this from my toolchain file to solve this problem??

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [July 2, 2025, 9:43am UTC](https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304/4 "2025-07-02T09:43:46Z")

</div>

I am suggesting, that you have set it to ` ${CMAKE_C_COMPILER}` which is set to `/opt/riscv/freestanding/multilib/bin/riscv64-unknown-elf-gcc` which contains no placeholders at all so this is the only thing that will end up as linker command (as you clearly discovered)

Since it seems you do not actually want to modify the linker command, just don’t set this variable.  
BTW: You also set `CMAKE_LINKER` two times to different values…

---

<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: [July 2, 2025, 10:29am UTC](https://discourse.cmake.org/t/cmake-link-txt-generated-during-cmake-configuration-does-not-contain-any-link-flags-or-source-files-when-using-riscv-toolchain/14304/5 "2025-07-02T10:29:58Z")

</div>

You use CMake like a Makefile but all the custom commands are not needed. Just use the built-in ASM language support.
