Building clang on Ubuntu Linux 12.04

Background

For a long time, the LLVM project used the various GCC front-ends with their compiler.  However, at the prompting of Apple who was looking for a faster compiler, as well as one unencumbered by the GPL 3 license, the clang project was begun in 2007.  Since then, clang has become a full featured C and C++ compiler. While installing the compiler on current versions of Linux isn’t quite the chore that GCC is, there are still a number of complications.  Below are my notes on installing the compiler on the  Ubuntu 12.04 (Precise) Linux distribution on a 64-bit (x86_64) machine.  Note that the compilers can still generate 32-bit code (using the -m32 switch).

Prerequisites

clang is written in C++ so you will need to have the g++ package installed.

trunk

The current development version of clang works pretty much right out of the box without tweaks. Check out the current sources from the subversion repository:

svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd clang/tools
svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
cd ../../../projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
cd ../../

Configure and build the optimized 64-bit native compiler, without extra debug assertion checks:

mkdir build
cd build
../llvm/configure –disable-assertions –enable-optimized –host=x86_64-linux-gnu
make
sudo make install

clang 3.1

The latest ‘released’ version of clang features full C99 and C++-03 support. Get the relevant source code:

wget http://llvm.org/releases/3.1/llvm-3.1.src.tar.gz
tar -xzf llvm-3.1.src.tar.gz
cd llvm-3.1.src/tools
wget http://llvm.org/releases/3.1/clang-3.1.src.tar.gz
tar -xzf clang-3.1.src.tar.gz
mv clang-3.1.src clang
cd ../projects
wget http://llvm.org/releases/3.1/compiler-rt-3.1.src.tar.gz
tar -xzf compiler-rt-3.1.src.tar.gz
mv compiler-rt-3.1.src compiler-rt
cd ../../

Configure and build:

mkdir
build
cd build
../llvm-3.1.src/configure –disable-assertions –enable-optimized –host=x86_64-linux-gnu
make
sudo make install

clang 3.0

The slightly older 3.0 version doesn’t have a separate compiler-runtime component to retrieve.  Also, the option to disable debugging assertions is –disable-asserts

wget http://llvm.org/releases/3.0/llvm-3.0.tar.gz
tar -xzf llvm-3.0.tar.gz
cd llvm-3.0.src/tools
wget http://llvm.org/releases/3.0/clang-3.0.tar.gz
tar -xzf clang-3.0.tar.gz
mv clang-3.0.src clang
cd ../../

Configure and build (same as with 3.1)

mkdir build
cd build
../llvm-3.0.src/configure –disable-asserts –enable-optimized –host=x86_64-linux-gnu
make
sudo make install

clang 2.9

This version doesn’t work with the STL headers from g++ 4.5 and newer.  As a result, you should install the g++-4.4 package. Also, the crt*.o files are not in the expected location for the compiler, which leads to link errors. Setup:

sudo apt-get install g++-4.4
sudo ln -s /usr/lib/x86_64-linux-gnu/crt*.o /usr/lib

Configure

wget http://llvm.org/releases/2.9/llvm-2.9.tgz
tar -xzf llvm-2.9.tgz
cd llvm-2.9/tools
wget http://llvm.org/releases/2.9/clang-2.9.tgz
tar -xzf clang-2.9.tgz
mv clang-2.9 clang
cd ../../

Build

mkdir build
cd build
../llvm-2.9/configure –disable-asserts –enable-optimized –host=x86_64-linux-gnu
make
sudo make install

clang 2.8

2.8 offers slightly weaker C++ support than 2.9, and needs the same tweaks to compile successfully.

sudo apt-get install g++-4.4
sudo ln -s /usr/lib/x86_64-linux-gnu/crt*.o /usr/lib
wget http://llvm.org/releases/2.8/llvm-2.8.tgz
tar -xzf llvm-2.8.tgz
cd llvm-2.8/tools
wget http://llvm.org/releases/2.8/clang-2.8.tgz
tar -xzf clang-2.8.tgz
mv clang-2.8 clang
cd ../../
mkdir build
cd build
../llvm-2.8/configure –disable-asserts –enable-optimized –host=x86_64-linux-gnu
make
sudo make install

clang 2.7

The second version of clang released, and the first with C++ support (enough for self-hosting).  Not only does it not work with the headers from g++ after version 4.4, but it won’t compile with newer versions either.

sudo apt-get install g++-4.4 sudo
ln -s /usr/lib/x86_64-linux-gnu/crt*.o /usr/lib
export CC=/usr/bin/gcc-4.4
export CXX=/usr/bin/g++-4.4
wget http://llvm.org/releases/2.7/llvm-2.7.tgz
tar -xzf llvm-2.7.tgz
cd llvm-2.7/tools
wget http://llvm.org/releases/2.7/clang-2.7.tgz
tar -xzf clang-2.7.tgz
mv clang-2.7 clang
cd ../../
mkdir build
cd build
../llvm-2.7/configure –disable-asserts –enable-optimized –host=x86_64-linux-gnu
make
sudo make install

clang 2.6 (1.0)

This is the first version of clang released.  It’s a fairly complete C compiler, but there is no C++ compiler (which means it can’t actually compile itself). All the same caveats as v2.7 apply.

sudo apt-get install g++-4.4
sudo ln -s /usr/lib/x86_64-linux-gnu/crt*.o /usr/lib
export CC=/usr/bin/gcc-4.4
export CXX=/usr/bin/g++-4.4
wget http://llvm.org/releases/2.6/llvm-2.6.tgz
tar -xzf llvm-2.6.tgz cd llvm-2.6/tools
wget http://llvm.org/releases/2.6/clang-2.6.tgz
tar -xzf clang-2.6.tgz
mv clang-2.6 clang
cd ../../
mkdir build
cd build
../llvm-2.6/configure –disable-asserts –enable-optimized –host=x86_64-linux-gnu
make sudo
make install

Older versions

Prior to version 2.6, LLVM relied on a modified version of the GCC front-end as its compiler. 

Comments are closed.