21

What's the best way to tell CMake to use the LLVM linker llvm-link instead of GNU ld as linker? When configuring a project with

CXX=clang++ cmake <args>

the default linker appears to be untouched, remaining usr/bin/ld (on Linux).

Is this possible without using a separate toolchain file?

mavam
  • 12,242
  • 10
  • 53
  • 87

2 Answers2

13

This turns out to be unrelated to CMake: clang++ uses the system linker by default. For example,

echo "#include <atomic>\n int main() { return 0; }" \
    | clang++ -x c++ -std=c++11 -stdlib=libc++ -

uses /usr/bin/ld to link the application. To change the linker to llvm-link, one needs to first emit LLVM byte code, and then call the linker, e.g.:

echo "#include <atomic>\n int main() { return 0; }" \
    | clang++ -x c++ -std=c++11 -stdlib=libc++ -S -emit-llvm -o - - \
    | llvm-link -o binary -

This bypasses /usr/bin/ld.

mavam
  • 12,242
  • 10
  • 53
  • 87
  • When you say the 'system linker' do you mean the one that it finds first in your `$PATH`, or is it hardcoded into clang to use the one in `/usr/bin/`? – Ephemera May 16 '14 at 02:37
  • Clang hardcodes the linker search path, AFAIK, and attempts to pick up the `ld` binary in other places besides your `$PATH`. I said "system linker" to refer to the standard linker of native object code as opposed to the LLVM byte code linker. – mavam May 17 '14 at 20:55
  • 2
    @orian: Check out this example: http://git.icir.org/binpacpp.git/blob/HEAD:/cmake/EnableLLVM.cmake – mavam May 17 '14 at 20:56
6

As of 3.4, clang looks for the linker (ld) at GCCInstallation.getParentLibPath() + "/../" + GCCInstallation.getTriple().str() + "/bin" before it looks for ld on the path. You should be able to put your linker in /usr/lib/gcc/<arch><sub>-<vendor>-<sys>-<abi>/<version>/ld and have it called by clang in 1 step. To specify this location manually, use the undocumented -B flag. Unfortunately, I don't believe there is a way to alter the name of the linker that is searched for so using ld.gold or lld is going to require a symlink at the aforementioned location.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47