Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LLVM linker when using Clang & CMake

Tags:

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?

like image 499
mavam Avatar asked Apr 23 '12 19:04

mavam


2 Answers

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.

like image 191
mavam Avatar answered Sep 21 '22 11:09

mavam


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.

like image 32
Alex Barker Avatar answered Sep 22 '22 11:09

Alex Barker