

These three variables control the (default) set of linker flags for executables, loadable modules, and shared libraries, respectively. Set(CMAKE_SHARED_LINKER_FLAGS_INIT "-fuse-ld=lld") Set(CMAKE_MODULE_LINKER_FLAGS_INIT "-fuse-ld=lld") Set(CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld") The best way to set these flags in the toolchain file is like so: # e.g. The best way to do this is by creating a toolchain file. Such flags need to be set before CMake's compiler detection routines run because it will try to compile a test binary. 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.

To call the linker, it goes through the configured compiler. (Thanks to bviktor for pointing out GCC's limitations for alternate linker selection.)ĬMake only gives you direct control over the compiler for each language. (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. GCC isn't as flexible, its -fuse-ld only accepts a limited set of possible arguments. 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. (BTW, the option is parsed ( -f) ( use-ld) ( =) ( lld), there's no "fuse" option to gcc.) Notes Easy-peasy, and CMake need not concern itself with such things at all. 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. 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" So, by far the simplest solution to this is to LET IT, and instead tell the compiler to run a different linker when called.

As Mabraham points out, CMake calls the compiler to do the linking.
