Tuesday, July 16, 2013

Clang 3.3 with 64-bit Ubuntu 12.04

Just documenting how I switch between various compilers on Linux with update-alternatives. I use update-alternatives, a tool to "maintain symbolic links determining default commands."

1. Grab appropriate clang 3.3 binaries from: http://llvm.org/releases/download.html

2. Unpack somewhere. (I unpacked to my ~/data directory using atool -x).

2. Install binaries with update-alternatives. For gcc 4.6 and clang 3.3, I did the following:

# install gcc 4.6
# "update-alternatives --install" takes a link, name, path and priority.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100
sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100

# install clang 3.3
# "update-alternatives --install" takes a link, name, path and priority.
sudo update-alternatives --install /usr/bin/gcc gcc ~/data/clang3.3/bin/clang 50
sudo update-alternatives --install /usr/bin/g++ g++ ~/data/clang3.3/bin/clang++ 50
sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100

3. I then use the following bash functions to switch compilers.

compiler_clang-3.3()
{
    sudo update-alternatives --set gcc ~/data/clang3.3/bin/clang
    sudo update-alternatives --set g++ ~/data/clang3.3/bin/clang++
    sudo update-alternatives --set cpp-bin /usr/bin/cpp-4.6
    update-alternatives --display gcc
    update-alternatives --display g++
    update-alternatives --display cpp-bin
}

compiler_gcc-4.6()
{
    sudo update-alternatives --auto gcc
    sudo update-alternatives --auto g++
    sudo update-alternatives --auto cpp-bin
    update-alternatives --display gcc
    update-alternatives --display g++
    update-alternatives --display cpp-bin
}

4. I also add clang and clang++ explicitly to my path in case something uses them explicitly.

mikesart@mikesart-rad:~/data/src/blah/build$ ll ~/bin/clang*
lrwxrwxrwx 1 mikesart mikesart 50 Jun 12 14:12 ~/bin/clang -> ~/data/clang3.3/bin/clang
lrwxrwxrwx 1 mikesart mikesart 52 Jun 12 14:12 ~/bin/clang++ -> ~/data/clang3.3/bin/clang++

5 - 100. MAJOR WARNING: Make sure you switch back to your default compiler before installing or updating pretty much anything. Various components like the NVIDIA driver will fail to build with anything but the system default, and then you're in for some good times.

No comments:

Post a Comment