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?
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With