Recently I wanted to support building of one of my projects (Life++) on Linux. Since this project makes use of some of the features of the fairly new C++11 Standard, I thought I’d use clang and libc++.
Because I really like clang and libc++ I thought it would be cool if i built clang, llvm and libc++ using clang and libc++. Sounds a little strange, doesn’t it? As i learned throughout my adventures, building libc++ also involved another decission to be made. Would I build it using the libsupc++ ABI or the libc++abi ABI. A quick google check made it clear for me, I wanted to have the libc++abi ABI since it was feature complete, modern, and written by the people behind llvm, clang and libc++.
WARNING: Some words of wisdom before we start. Depending on your machine
this process is going to take some time. I did this in a virtual machine
running Ubuntu 13.04 Alpha 2 on an early 2011 15” MacBook Pro featuring a 2.3
GHz Intel Core i7 CPU. It took me about 2h to get everything up and running.
Also this process installs headers libraries and binaries on your system. If
you want to keep your system clean and easy to maintain, please install it
somewhere else than in
/usr. You might go for
Remember however that this might require you to add those paths to your global
To follow this post you need to have some packages installed on your system. Please note that I did this on a Ubuntu Linux installation, so all the commands and package names might be Ubuntu specific. Please make sure, your system is up-to-date before attempting this installation. So on Ubuntu execute:
sudo apt-get update sudo apt-get upgrade
After you finished updating your system, you need to install some basic stuff to be able to compile llvm, clang and libc++. To install the required packages run:
sudo apt-get install g++ subversion cmake
You might want to add python-dev, swig and libedit-dev to bunch if you want to install lldb too. If you choose to do so, the command becomes
sudo apt-get install g++ subversion cmake swig python-dev libedit-dev
After this process is finished we can continue to prepare our building environment by checking out the source code. Please notice that you don’t want to check out the source for lldb right now, since this would only unnecessary increase the build time.
First you should create a directory in which you’re going to do the dirty work.
You can choose any directory you like, I chose to use
mkdir ~/Documents/clang cd ~/Documents/clang
The next step is to checkout the sources for llvm, clang, libc++ and libc++abi. We’re going to start with llvm. Simply type
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
and hit return. After the checkout completed change to the directory
llvm/tools and checkout the clang source code:
cd llvm/tools svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
as a next step, we need to check out libc++ and libc++abi. Simply change back to our root working directory and check out the sources:
cd ~/Documents/clang svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
To finish up our preparations we’re going to create some directories to hold the intermediate build files, so if we mess up we can simply clean them out and start over. To do so, run:
mkdir clang_build mkdir libcxx_build
Round 1: Initial building
In the first round of building, we’re going to build llvm and clang using
g++. This is a necessary evil. So to start, make sure you’re in the root
working directory and change to the
cd ~/Documents/clang cd clang_build
From inside of this directory run the following command:
../llvm/configure --prefix=/usr --enable-optimized --enable-targets=host --with-c-include-dirs="/usr/include/x86_64-linux-gnu/c++/4.7/.:/usr/include/c++/4.7:/usr/include/c++/4.7/backward:/usr/lib/gcc/x86_64-linux-gnu/4.7/include:/usr/local/include:/usr/include/x86_64-linux-gnu:/usr/include"
WARING: The paths used with the –with-c-include-dirs option are installation dependend. You can easily grab them via the following command: echo | g++ -Wp,-v -x c++ - -fsyntax-only Please make sure to use the correct paths in to command above!
After this command finishes, just type
make -j 8
8 with twice number of processors / cores on your system and
finish off with:
sudo make install
Congratulations, you made it through the first round, go get yourself some coffee or some other treat!
Round 2: Building libc++
Welcome back to the second round of our adventure game called “Building clang, LLDB and libc++ on Ubuntu”. In this round we’re going to focus on libc++ and libc++abi. libc++ is a modern implementation of the C++ Standard Library building on years of experience in using C++. It is written by the same people behind the LLVM project and it’s a feature complete implementation of the C++11 standard. We’re also going to use libc++abi as our ABI for the same reasons.
To get us going, please change to the directory
libcxxabi/lib. As always,
make sure to start from the root working directory:
cd ~/Documents/clang cd libcxxabi/lib
From there run the script called buildit:
After this script finishes processing, copy the newly build libc++abi.so.1.0
to a place where our linker can find it, for example
/usr/lib, and create
cp libc++abi.so.1.0 /usr/lib ln -s /usr/lib/libc++abi.so.1.0 /usr/lib/libc++abi.so.1 ln -s /usr/lib/libc++abi.so.1 /usr/lib/libc++abi.so
Now that our linker can find our newly built ABI its time to build libc++. To
do so, change the directory to
cd ~/Documents/clang cd libcxx_build
And run the following command:
CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_LIBCXXABI_INCLUDE_PATHS="../libcxxabi/include" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr $HOME/Documents/clang/libcxx
The next step is to build and install libc++ using these two commands:
make sudo make install
Well done! You’ve successfully built libc++ using the libc++abi! Again, get yourself a treat!
FINAL ROUND: Building LLVM, clang - and possibly LLDB - for good
First of all, you’re great! You’ve made it into the final round! Now all eyes are on you, and I’m sure you’re going to make it. Literally. If you choose to build LLDB, and I really recommend you do so, you need to check out its source:
cd ~/Documents/clang/llvm/tools svn co http://llvm.org/svn/llvm-project/lldb/trunk lldb
Whether or not you chose to build LLDB, head on over to the
directory and clean it out:
cd ~/Documents/clang/clang_build rm -rf *
Afterwards configure the build environment:
CC="/usr/bin/clang" CXX="/usr/bin/clang++" LDFLAGS="-lc++abi" ../llvm/configure --prefix=/usr --enable-optimized --enable-libcpp --enable-cxx11 --enable-targets=host --with-c-include-dirs="/usr/include/:/usr/include/c++/v1:/usr/lib/clang/include:/usr/local/include:/usr/include/x86_64-linux-gnu"
and build the hell out of it:
make -j 4
NOTE: It may be that you encounter an error during the build process of
Mangled.cpp. If you do, you need to add
#include <cstdlib> - without the
quotes - to the list of include files in this file. After you’ve done so,
simply rerun make.
When make finishes simply install your new toolset via:
sudo make install
and enjoy your newly built tools!
Congratulations, you’ve made it through all three rounds! You’re now in
possesion of a completely self-hosting toolchain build on LLVM and clang.
As a bonus, you are awarded the
Buildmaster badge in gold! Thanks for
reading this post and of course for taking the time to follow it, I wish you
all the best with your new toolchain!
Subscribe via RSS