# Using the Link time Optimizations with the gold linker



## ervd (Jun 22, 2015)

I have been trying to enable the gold linker on FreeBSD to use the link time optimizations. I made gold from the binutils under /usr/ports. After building binutils using `make -k install clean` iI got ld under /usr/bin and in the directory /usr/local/bin iI got ld, ld.gold and ld.bfd.

Now while trying to use link time optimization for the simple example programs here http://llvm.org/docs/GoldPlugin.html (a.c and b.c under the heading 'Examples of Link Time Optimization') iI entered the four commands as follows:

```
clang -flto a.c -c -o a.o
ar q a.a a.o
clang b.c -c -o b.o
clang -flto a.a b.o -o main
```

I got the following error:

```
usr/bin/ld: unrecogonized option '-plugin'
usr/bin/ld: use the --help option for usage information
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

Is there the problem with the linker that ld.gold is not being called. Should I replace the ld with ld.gold? Does the linker looks in the right directiory for the .so plugins?

The LLVMgold.so and libLTO.so shared objects are in the directory /usr/local/llvm-devel/lib/.
I cannot find the directory where clang is installed. I am not sure where to make the bfd-plugins directory and add the symlinks to LLVMgold.so and libLTO.so.

I am using freebsdFreeBSD 10.1 release. How to enable the gold linker for link time optimizations?
aAlso how can I enable it to be the default linker?


----------



## wblock@ (Jun 22, 2015)

Sorry, no idea about the linker.  But using -k with `make` is a bad sign.  I would look at fixing those problems first, because telling make(1) to just ignore errors and proceed is not a recipe for solid code.


----------



## sprattus (Sep 16, 2015)

A short term solution of the ld/ld.gold problem is to use symbolic links.
On FreeBSD 10.2, the path-setting shown below compiles & links the test example.


```
$ file llvm-env/*
llvm-env/ar:      symbolic link to /usr/local/llvm-devel/bin/llvm-ar
llvm-env/clang:   symbolic link to /usr/local/llvm-devel/bin/clang
llvm-env/ld:      symbolic link to /usr/local/bin/ld.gold

$ export PATH="`realpath llvm-env`:$PATH"
$ clang -flto a.c -c -o a.o
$ ar q a.a a.o
$ clang b.c -c -o b.o
$ clang -flto a.a b.o -o main
```


----------

