Building Clang and libc++ on Ubuntu Linux
Clang with libc++ represents today a viable alternative to GCC for C and C++ programmers. I make no claim that Clang with libc++ is necessarily superior to GCC with libstdc++, but it is definitely a more feature complete C++11 alternative. According to the libc++ website, libc++ is 100% complete C++11 implementation on Apple’s OS X and, from my tests, works equally well on Linux. Even if you plan to ship your C++ application compiled with GCC, I think it is a good test to check your app for errors by building it with a different compiler and library.
In this short post, I’m going to show you how to build the latest Clang and libc++ on a Linux box. I’ve tested this procedure on Ubuntu 12.04.1, Ubuntu 12.10 and Linux Mint 14.
Let’s start by updating your Linux system (this is a recommended but optional step), open a Terminal and paste the next two lines:
sudo apt-get update sudo apt-get upgrade
Now, let’s install some additional, but necessary, apps:
sudo apt-get install g++ subversion cmake
Next step, is to get the latest version of Clang and LLVM:
cd ~ mkdir Clang && cd Clang 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
and to build Clang and LLVM:
cd .. cd .. mkdir build && cd build ../llvm/configure --prefix=/usr/clang_3_3 --enable-optimized --enable-targets=host make -j 8
Let’s install Clang:
sudo make install
In order to be able to use Clang we’ll need to add it to our system path, you can do this with:
export PATH=/usr/clang_3_3/bin:$PATH
or, to permanently add Clang to your system path, paste the above line at the end of your .bashrc file. If you don’t know how to find .bashrc, try this:
cd ~ gedit ~/.bashrc
For building libc++, we’ll need the include path used by your system C++ compiler and the target platform, we can get these with:
echo | g++ -Wp,-v -x c++ - -fsyntax-only
This is the result of the above line on my system:
sol@sols:~$ echo | g++ -Wp,-v -x c++ - -fsyntax-only ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /usr/include/c++/4.6 /usr/include/c++/4.6/x86_64-linux-gnu/. /usr/include/c++/4.6/backward /usr/lib/gcc/x86_64-linux-gnu/4.6/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list. sol@sols:~$
The first highlighted line from above gives us the include path and the second line the target platform (a 64 bits Linux system). As a side note, on Ubuntu 12.10 you will get 4.7 instead of 4.6!
Let’s get and install the latest libc++:
cd ~/Clang svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx mkdir build_libcxx && cd build_libcxx CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libsupc++ -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS="/usr/include/c++/4.6/;/usr/include/c++/4.6/x86_64-linux-gnu/" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr $HOME/Clang/libcxx make -j 8 sudo make install
If you build libc++ on a different system, remember to change the include path for your system and the target machine accordingly. For e.g. on Ubuntu 12.10 or Linux Mint 14 (64 bits), the above highlighted line will became:
CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libsupc++ -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr $HOME/Clang/libcxx
Now, you should be able to compile any valid C++11 program with:
clang++ -std=c++11 -stdlib=libc++ <your_program_name>