22

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?

1

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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/?
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.