How to choose the LLVM version clang is using?

11,421

Solution 1

Clang is statically linked with LLVM, so you can't separate the two in the sense you try to imply here.

What you can do is install Clang 2.9 separately, following the usual compiler installation instructions, and compile your project with that.

Solution 2

I tried many options and here what works best for me.

Remove all existing version of llvm/clang. To get a full list run this:

dpkg -l | egrep -i "clang|llvm"

In my case I had to keep at least "libllvm5.0:amd64" because many packages depend on it.

Download a prebuilt version from http://releases.llvm.org/download.html and verify the signature.

gpg --verify clang+llvm-[VERSION].tar.xz.sig

Extract the content to some folder.

sudo tar -xf archive.tar -C /opt/clang+llvm-[VERSION]

Add this line at the complete end of your .bashrc or run manually to change version.

export PATH="/opt/clang+llvm-[VERSION]/bin:$PATH"

Another option could be to use update-alternatives.

#!/bin/bash -ex

DIR=/opt/clang+llvm-[VERSION]/bin

sudo update-alternatives \
--install /usr/bin/clang        clang       "$DIR/clang" 100 \
--slave   /usr/bin/clang++      clang++     "$DIR/clang++"

Then use this command if more then 1 version is installed.

sudo update-alternatives --config clang

Check if the system is correctly configured.

llvm-config --prefix --version
clang --version
Share:
11,421
Borph
Author by

Borph

Updated on June 26, 2022

Comments

  • Borph
    Borph almost 2 years

    Maybe it's too obvious, but I don't see the answer yet:

    I try to compile my project under Ubuntu with clang, but unfortunately llvm is crashing with a stack dump.

    $ clang --version
    Ubuntu clang version 3.0-6ubuntu3 (tags/RELEASE_30/final) (based on LLVM 3.0)
    Target: i386-pc-linux-gnu
    Thread model: posix
    

    I would like to give LLVM 2.9 a try, installed it, but how can I choose it? E.g.:

    $ ll /usr/bin/llvm-ar*
    lrwxrwxrwx 1 root root 27 Sep 12 16:43 /usr/bin/llvm-ar -> ../lib/llvm-3.1/bin/llvm-ar*
    lrwxrwxrwx 1 root root 27 May 21  2012 /usr/bin/llvm-ar-2.9 -> ../lib/llvm-2.9/bin/llvm-ar*
    lrwxrwxrwx 1 root root 27 Aug 27 20:31 /usr/bin/llvm-ar-3.0 -> ../lib/llvm-3.0/bin/llvm-ar*
    lrwxrwxrwx 1 root root 27 Aug 28 17:49 /usr/bin/llvm-ar-3.1 -> ../lib/llvm-3.1/bin/llvm-ar*
    $ ll /usr/bin/clang*
    -rwxr-xr-x 1 root root 12686228 Apr 25  2012 /usr/bin/clang*
    lrwxrwxrwx 1 root root        5 Apr 25  2012 /usr/bin/clang++ -> clang*
    

    Clang as a command exists only once, and I didn't see a command line option of it to choose the llvm backend. Am I missing something?

  • Borph
    Borph about 11 years
    But I just have one clang, and in the ubuntu repositories I don't find any other. I guess that means a manual installation :-(
  • Eli Bendersky
    Eli Bendersky about 11 years
    @Borph: manual installation is easy since you can find pre-compiled Ubuntu binaries on the project's website