C++ – Unable to Change Default Linker in CMake

cclangcmakelinkerlinux

I am trying to change my linker to ld.gold so that I can build LLVM and
CLANG faster. I have changed my environment variable with:

export LD=ld.gold

and I have changed CMAKE_LINKER in ccmake to /usr/bin/ld.gold. However,
when I generate the files, my linker is detected as GNU ld. Running top
during compilation confirms that ld is running rather than gold.

When editing the CMake Link Executable variable to:

cmake -DCMAKE_LINKER=/usr/bin/ld.gold -DCMAKE_CXX_LINK_EXECUTABLE="<CMAKE_LINKER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>" -G "Unix Makefiles" ../llvm

I receive the following error:

/usr/bin/ld.gold: -Werror=date-time: unknown option
/usr/bin/ld.gold: use the --help option for usage information
utils/PerfectShuffle/CMakeFiles/llvm-PerfectShuffle.dir/build.make:94: recipe for target 'bin/llvm-PerfectShuffle' failed
make[2]: *** [bin/llvm-PerfectShuffle] Error 1
CMakeFiles/Makefile2:13983: recipe for target 'utils/PerfectShuffle/CMakeFiles/llvm-PerfectShuffle.dir/all' failed

I'm on Ubuntu 16.04, but I have had the same problem on Arch Linux.

Thank You.

Best Answer

CMAKE_EXE_LINKER_FLAGS=-fuse-ld=gold will pass -fuse-ld=gold to the compiler, which will properly use the gold linker.

Related Topic