How to change the linker of clang

  1. Build clang with GCC-4.9.2, when compling other application s with clang/clang++, the default linker is ld, can I replace it with other linker tool, if we can, how to do it?

I.E. can we use collect2 instead?

  1. how to specify the path, if we do not use the default vertion GCC?

Best Regards!
Eric Lew

1) Build clang with GCC-4.9.2, when compling other application s with
clang/clang++, the default linker is ld, can I replace it with other linker
tool, if we can, how to do it?

I.E. can we use collect2 instead?

You could symlink ld to point to collect2.

For example I use gold and point to:
ls -la /usr/local/bin/ld:
/usr/local/bin/ld -> /usr/bin/gold

2) how to specify the path, if we do not use the default vertion GCC?

You can specify which version of GCC when you run cmake by adding the flags:
-DCMAKE_C_COMPILER=<path> -DCMAKE_CXX_COMPILER=<path>

That’s what I used to do but each Ubuntu update would revert the symlink. That’s when I found about -fuse-ld=gold, I’d expect it to work with other linkers as well.

The -B option tells clang where to look for the linker. So you could try:

clang -B /path/to/other/linker/ …

Sandeep

For #1: Consider also the -B option to specify a path prefix to consult for compiler-related sub-programs (e.g., GNU binutils) if the executables / symlinks etc. have similar names.

Brian

Also maybe clang could support -B option like gcc have [1], that allow
you to change prefix for executables and other compiler stuff.

[1]: Directory Options (Using the GNU Compiler Collection (GCC))

It does.

Joerg

Didn’t we upstream the support for -fuse-ld=foo, which would use ld.foo in your default linker search path, so if you install gold as /usr/bin/ld.gold (as, I believe, is the default), it will work with -fuse-ld=gold?

David

Hi David,