Issue with Toolchain (cross compiler) Configuration in CMakeLists.txt

Hello
I am encountering an issue with my CMake configuration, and I’m seeking your expertise to help me understand and resolve it.

I have a CMakeLists.txt file where I’ve set the toolchain(cross compiler host: windows , target Linux) to use x86_64-pc-elf-gcc.exe for C compiler, x86_64-pc-elf-g++.exe for C++ compiler, and x86_64-pc-elf-ld.exe as the linker:

cmake_minimum_required(VERSION 3.25)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_HOST_SYSTEM_NAME Windows)

set(CMAKE_SYSTEM_PROCESSOR x86_64)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_COMPILER gcc.exe)
set(CMAKE_CXX_COMPILER x86_64-pc-elf-g++.exe)
set(CMAKE_LINKER x86_64-pc-elf-ld.exe)

project("YourProjectName" VERSION 1.0.0)

set(SOURCES 
        ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src/a.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src/b.cpp
)

add_executable(a_obj ${SOURCES})

However, during the build process, the linker seems to default to /x86_64-pc-elf/bin/ld.exe,
but I want to use x86_64-pc-elf-ld.exe. why it is not selected?
resulting in the error:

[main] Building folder: odkvscodex 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/odkvscodex/build --config Debug --target all --
[build] [3/4  25% :: 0.468] Building CXX object CMakeFiles/a_obj.dir/src/b.cpp.o
[build] [3/4  50% :: 0.469] Building CXX object CMakeFiles/a_obj.dir/src/a.cpp.o
[build] [3/4  75% :: 1.148] Building CXX object CMakeFiles/a_obj.dir/src/main.cpp.o
[build] [4/4 100% :: 1.412] Linking CXX executable a_obj
[build] FAILED: a_obj 
[build] cmd.exe /C "cd . && D:\x86_64_gcc_pc_elf_11.3.0\bin\x86_64-pc-elf-g++.exe   CMakeFiles/a_obj.dir/src/main.cpp.o CMakeFiles/a_obj.dir/src/a.cpp.o CMakeFiles/a_obj.dir/src/b.cpp.o -o a_obj   && cd ."
[build] d:/x86_64_gcc_pc_elf_11.3.0/bin/../lib/gcc/x86_64-pc-elf/11.3.0/../../../../x86_64-pc-elf/bin/ld.exe: cannot find crt0.o: No such file or directory
[build] collect2.exe: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/odkvscodex/build --config Debug --target all -- exited with code: 1
[driver] Build completed: 00:00:01.564
[build] Build finished with exit code 1

I’m puzzled about why the specified linker is not being used. Any insights or suggestions on how to troubleshoot and resolve this issue would be greatly appreciated.

Thank you in advance for your assistance!

I couldn’t find CMAKE_LINKER in the CMake documentation. I recall hearing that CMake normally uses the compiler for the link step.

Maybe this can point you in the right direction.

1 Like

Yes.

The triples are the same; what is the difference? I believe you’ll need to set something up so that -fuse-ld= can find what you want.

1 Like