How to set Clang 3.9 as the default in Zesty?

Zesty ships with multiple versions of Clang. The clang Install clang package depends on both clang-3.9 Install clang-3.9 and clang-4.0 Install clang-4.0. It appears that Clang 4 is used by default:

$ clang --version
clang version 4.0.0-1ubuntu1 (tags/RELEASE_400/rc1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

However, I need Clang 3.9 to be the default version. I am unable to compile UnrealEngine because of this:

UnrealBuildTool Exception: ERROR: This version of the Unreal Engine can only be
    compiled with clang 3.9, 3.8, 3.7, 3.6 and 3.5. clang 4.0.0 may not build it -
    please use a different version.

How can I go about this?

Asked By: Nathan Osman

||

This answer pointed me in the right direction:

sudo update-alternatives --install 
    /usr/bin/clang++ clang++ /usr/lib/llvm-3.9/bin/clang++ 100
sudo update-alternatives --install 
    /usr/bin/clang clang /usr/lib/llvm-3.9/bin/clang 100

After running those two commands, the build was able to continue.

Answered By: Nathan Osman

Adding to the accepted answer:

If you have multiple versions of clang, it may be in your best interest to make clang++ dependent on clang so that when you switch to a different clang version and the version of clang++ follows suit.

You can do this using the --slave option of update-alternatives. So something like this:

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.9 100 
   --slave /usr/bin/clang++ clang++ /usr/bin/clang++-3.9

You can of course do it for other versions:

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-4.0 100 
   --slave /usr/bin/clang++ clang++ /usr/bin/clang++-4.0

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-5.0 100 
   --slave /usr/bin/clang++ clang++ /usr/bin/clang++-5.0

To switch versions, you just type:

sudo update-alternatives --config clang

Enter a selection and both clang and clang++ will be automatically switched to the same versions.

The same idea applies to GCC if you also have multiple versions of that, you can use this method to configure gcc and g++.

Errors:

update-alternatives: error: alternative clang++ can’t be slave of
clang: it is a master alternative

You may get this error when you try to run the above commands. No worries, it just means that you already configured clang++ on it’s own as an alternative, so you will need to remove that alternative before the above will work. You can do so with the command:

sudo update-alternatives --remove clang++ /usr/bin/clang++-3.9

Do this for each version of clang++, then after removing them all, try again.

Sources:

Answered By: smac89
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.