Clang

is a compiler for C family of languages using LLVM as a backend and optimizer. It aims to be GCC compatible yet stricter.

Common notes
Please note that although clang tries to be compatible with gcc, not all packages build correctly with it. Although some of them simply fail to build, there are also cases when packages build fine but fail horribly when run.

The main place for looking up known failures with clang is. If you hit one not reported on our Bugzilla already, please open a new bug report and make it block 408963.

If you'd like to try to build parts of your system with clang, the first good thing to do would be enabling tests via FEATURES=test. Note that many developers don't actually care about their own tests, and as a result you are likely to get many false positives. Because of this, you may need to compare the results with a gcc build.

Use of GNU extensions without proper -std=
Some packages tend to use GNU extensions in their code without specifying -std= appropriately. GCC allows that usage, yet clang disables some of more specific GNU extensions by default.

If a particular package relies on such extensions being available, you will need to append the correct -std= flag to it:


 * -std=gnu89 for C89/C90 with GNU extensions,
 * -std=gnu99 for C99 with GNU extensions,
 * -std=gnu++98 for C++:1998 with GNU extensions.

A common symptom of this problem are multiple definitions of inline functions like this:

/usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_add' /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_add_1' /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_cmp' /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here /usr/bin/x86_64-pc-linux-gnu-ld: error: ../mpi/.libs/libmpi.a(mpi-bit.o): multiple definition of '_gcry_mpih_sub' /usr/bin/x86_64-pc-linux-gnu-ld: ../mpi/.libs/libmpi.a(mpi-add.o): previous definition here

This is because clang uses C99 inline rules by default which do not work with gnu89 code. To work around it, you most likely have to pass -std=gnu89.

Using clang with portage
Although Gentoo package tree is not designed to be used with compiler other than GCC, clang can be enforced on most of the packages through CC and CXX variables.

Please note, however, that many of Gentoo packages still don't build with clang and a few don't work correctly after being built. That's why we suggest using file to enable the use of clang per-package.

In order to do that, first create a new environment override to use:

Then you can enable use of clang for packages using /etc/portage/package.env file:

If you have installed, the  file could be modified using:

Enabling link-time optimizations
The link-time optimization feature defers optimizing the resulting executables to linking phase. This can result in better optimization of packages but isn't standard behavior in Gentoo yet. In order to use LTO we must first install LLVM with the gold linker plugin so clang can pass the -plugin flag to the linker.

Set the gold flag in the llvm package so portage knows to include the gold plugin.

Now emerge llvm and check the use flags to make sure your changes went into effect.

We must set gold as the default linker since it allows clang to pass the -plugin flag. By default, Gentoo uses the bfd linker. As of July 2016, the bfd linker bundled with binutils does not support passing the -plugin as seen below.

Using the sys-devel/binutils-config package, we'll set ld to default to ld.gold instead of ld.bfd.

Once ld is set to gold, check the -plugin flag to see if it works.

Great! It works. If you want to switch back to the default linker change the command from ld.gold to ld.bfd.

Now that we're done setting up the gold linker, we can create a new environment override profile for LTO enabled clang. We do this by passing the -flto flag to clang which in turn passes the -plugin flag to the linker. You need to set optimization flags when linking because that's where clang needs them. Linker flags should have -Wl, preceding them since they're being invoked by the compiler and not run manually.

As an alternative, LLVM provides their own ar, nm, and ranlib. You're free to use them and may or may not get more mileage over using the standard ar, nm, and ranlib since they're intended to handle LLVM bitcode which clang produces when using the -flto flag.

Now you can set package.env overrides using LTO enabled clang.

Using clang with distcc
In order to use clang on distcc client, additional symlinks have to be created in /usr/lib*/distcc/bin:

Using clang with ccache
Automatic with `>=ccache-3.9-r3`, when `clang` is already installed at emerge time.