Reputation: 1991
I want to setup a custom toolchain with CMake. I've set the compiler but I don't know how to set the linker. This error is reported because CMake try to use the compiler to link:
The C compiler "xgcc.exe" is not able to compile a simple test program.
Here there is a snippet of my toolchain file
# specify the cross compiler
set(CMAKE_C_COMPILER xgcc.exe)
set(CMAKE_CXX_COMPILER xgcc.exe)
Upvotes: 87
Views: 137916
Reputation: 69
Finally, implemented in CMake 3.29 via CMAKE_<LANG>_USING_LINKER_<TYPE>
.
A custom linker type can be defined, usually in a toolchain file:
set(CMAKE_LINKER_TYPE lld_launcher)
set(CMAKE_C_USING_LINKER_lld_launcher "-fuse-ld=/path/to/lld-launcher.sh")
set(CMAKE_C_USING_LINKER_MODE FLAG)
Upvotes: 2
Reputation: 3016
The link command line is set in Modules/CMake{C,CXX,Fortran}Information.cmake and defaults to using the compiler, not CMAKE_LINKER
(see source code). This can be changed by replacing the rule that builds the link command line, which lives in variables CMAKE_CXX_LINK_EXECUTABLE
(and friends). NB that variable does not indicate the path to the linker executable; it says how to link an executable!
Edit: nicer approaches to customizing this behaviour can be found in answers from usr123467 and FeRD.
One approach is to set that rule to use the linker, e.g.
cmake -DCMAKE_LINKER=/path/to/linker -DCMAKE_CXX_LINK_EXECUTABLE="<CMAKE_LINKER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
See also this post from CMake mailing list and this one - this also makes a natural place to prepend a linker modifier to another linker.
Upvotes: 52
Reputation: 23294
Beginning with version 3.29, CMake offers a way to set the linker.
CMAKE_LINKER_TYPE
to one of the predefined variables LLD
, GOLD
, MOLD
, BFD
, MSVC
, or the more exotic ones. For more details have a look into the documentation.CMAKE_<LANG>_USING_LINKER_<TYPE>
with either the path to the linker or the flag for the compiler. Tell CMake your choice by setting the variable CMAKE_<LANG>_USING_LINKER_MODE
(Documentation).Upvotes: 16
Reputation: 2084
As Mabraham points out, CMake calls the compiler to do the linking. So, by far the simplest solution to this is to LET IT, and instead tell the compiler to run a different linker when called.
Which, as noted in this other answer — but now it's even a documented option in gcc --help=common
— is as easy as:
cmake -DCMAKE_CXX_FLAGS="-fuse-ld=lld"
g++
or clang++
will get passed the -fuse-ld=lld
1 flag on every call, and when they do any linking they'll use the specified command instead of the built-in default. Easy-peasy, and CMake need not concern itself with such things at all.
(BTW, the option is parsed (-f
) (use-ld
) (=
) (lld
), there's no "fuse" option to gcc
.)
When using Clang, lld
can be replaced with whatever other linker command you want to use, like ld.exe
, ld.gold
, mingw32/bin/ld.exe
, etc.
GCC isn't as flexible, its -fuse-ld
only accepts a limited set of possible arguments. (They're listed in the gcc --help=common
output, as of GCC 12.2.1 the list is: bfd
, gold
, lld
, or mold
.) It will invoke the first matching ld.foo
executable it finds on the PATH
. (Thanks to bviktor for pointing out GCC's limitations for alternate linker selection.)
Upvotes: 48
Reputation: 1648
Here is a CMake function which sets linker based on some predefined arbitrary rules (Clang -> lld-version or lld, GCC -> gold).
The important parts:
lld-version
which matches the Clang compiler version (ex. lld-13 if Clang 13.x.x is used), falls back to lld
if not found add_link_options("-fuse-ld=lld-${CLANG_VERSION_MAJOR}")
gold
: add_link_options("-fuse-ld=gold;LINKER:--threads,--thread-count=${HOST_PROC_COUNT}")
The example is a bit too long because of comments, logs and custom logic, but it is self-contained and could be useful staring point for beginners.
function(select_best_linker) #lld for Clang and GNU gold for GCC
if (UNIX AND NOT APPLE)
include(ProcessorCount)
ProcessorCount(HOST_PROC_COUNT)
if(${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
# By default LLD uses all system threads.
# This could be tweaked for versions 11+ (--threads=1), but cannot be disabled for older versions
# add_link_options("-fuse-ld=lld-${CLANG_VERSION_MAJOR};LINKER:--threads=${HOST_PROC_COUNT}") #LLD>=11
# add_link_options("-fuse-ld=lld;LINKER:--threads")#LLD <= 10 this is the default state
string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
list(GET VERSION_LIST 0 CLANG_VERSION_MAJOR) #extract major compiler version
find_program(LLD_PROGRAM_MATCH_VER lld-${CLANG_VERSION_MAJOR}) #search for lld-13 when clang 13.x.x is used
find_program(LLD_PROGRAM lld) #else find default lld
if (LLD_PROGRAM_MATCH_VER) #lld matching compiler version
message(STATUS "Set linker to LLD (multi-threaded): ${LLD_PROGRAM_MATCH_VER}")
add_link_options("-fuse-ld=lld-${CLANG_VERSION_MAJOR}")
elseif(LLD_PROGRAM) #default lld
message(STATUS "Set linker to LLD (multi-threaded): ${LLD_PROGRAM}")
add_link_options("-fuse-ld=lld")
endif(LLD_PROGRAM_MATCH_VER)
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
find_program(GNU_GOLD_PROGRAM gold)
if (GNU_GOLD_PROGRAM)
message(STATUS "Set linker to GNU gold: ${GNU_GOLD_PROGRAM}, using threads: ${HOST_PROC_COUNT}")
add_link_options("-fuse-ld=gold;LINKER:--threads,--thread-count=${HOST_PROC_COUNT}")
endif(GNU_GOLD_PROGRAM)
endif(${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
endif(UNIX AND NOT APPLE)
endfunction(select_best_linker)
Tested on:
Upvotes: 3
Reputation: 19826
CMake only gives you direct control over the compiler for each language. To call the linker, it goes through the configured compiler. This means that there is no universal way to set the linker in CMake, you must configure your compiler to use the linker you intend.
Such flags need to be set before CMake's compiler detection routines run because it will try to compile a test binary. The best way to do this is by creating a toolchain file. The best way to set these flags in the toolchain file is like so:
# e.g. to use lld with Clang
set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld")
These three variables control the (default) set of linker flags for executables, loadable modules, and shared libraries, respectively. There is no need to handle CMAKE_STATIC_LINKER_FLAGS_INIT
(for static libraries) here because the archiver is invoked, rather than the linker.
You can then set the toolchain file when you first run CMake by setting -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake
at the command line. As of CMake 3.21, you will be able to pass --toolchain /path/to/toolchain.cmake
instead (which is entirely equivalent, but a little less typing).
Upvotes: 11
Reputation: 28751
I had success with doing
add_link_options("-fuse-ld=lld")
It is a variation on the previous answers here. The difference is the CMake command I use to set the flag.
Adding it to CMAKE_CXX_FLAGS
has the disadvantage of then also having to add -Wno-unused-command-line-argument
as the flags get also added to compilation commands, not only to linking ones.
The disadvantage of CMAKE_SHARED_LINKER_FLAGS
is that you have to add it multiple times, to _SHARED_
, _EXE_
, and maybe I forgot something.
Upvotes: 18
Reputation: 79
There is another way to do it, gcc has a "-fuse-ld" option, you can set LINKER_FLAGS in CMakeLists.txt like these:
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
then the custom specified linker should be invoked.
Upvotes: 0
Reputation: 730
For completeness, another full-proof option is to just link /usr/bin/ld
to ld.gold by running
sudo ln -sf /usr/bin/x86_64-linux-gnu-ld.gold /usr/bin/ld
as suggested here
Upvotes: 3
Reputation: 1991
I have to use CMAKE_CXX_LINK_EXECUTABLE, CMAKE_C_LINK_EXECUTABLE variable:
SET(CMAKE_C_LINK_EXECUTABLE "c:\\MoSync\\bin\\pipe-tool.exe")
Upvotes: 5
Reputation: 30419
Set the variable ${CMAKE_LINKER} either in CMakeCache.txt or after ccmake . under advanced options.
Upvotes: 4