Solarian Programmer

My programming ramblings

Building Clang and libc++ on Ubuntu Linux

Posted on January 17, 2013 by Sol

Updated on 26 January 2014 for Ubuntu 12.04.4 and Clang 3.4.

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.4, 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:

1 sudo apt-get update
2 sudo apt-get upgrade

Now, let’s install some additional, but necessary, apps:

1 sudo apt-get install g++ subversion cmake

Next step, is to get the latest version of Clang and LLVM:

1 cd ~
2 mkdir Clang && cd Clang
3 svn co llvm
4 cd llvm/tools
5 svn co clang

and to build Clang and LLVM:

1 cd ..
2 cd ..
3 mkdir build && cd build
4 ../llvm/configure --prefix=/usr/clang_3_4 --enable-optimized --enable-targets=host --disable-compiler-version-checks
5 make -j 8

The above step will take some time …

Let’s install Clang:

1 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:

1 export PATH=/usr/clang_3_4/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:

1 cd ~
2 gedit ~/.bashrc
3 . .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:

1 echo | g++ -Wp,-v -x c++ - -fsyntax-only

This is the result of the above line on my system:

 1 sol@sols:~$ echo | g++ -Wp,-v -x c++ - -fsyntax-only
 2 ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
 3 ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include"
 4 #include "..." search starts here:
 5 #include <...> search starts here:
 6  /usr/include/c++/4.6
 7  /usr/include/c++/4.6/x86_64-linux-gnu/.
 8  /usr/include/c++/4.6/backward
 9  /usr/lib/gcc/x86_64-linux-gnu/4.6/include
10  /usr/local/include
11  /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
12  /usr/include/x86_64-linux-gnu
13  /usr/include
14 End of search list.
15 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++:

1 cd ~/Clang
2 svn co libcxx
3 mkdir build_libcxx && cd build_libcxx
4 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
5 make -j 8
6 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:

1 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:

1 clang++ -std=c++11 -stdlib=libc++ <your_program_name>

Let’s test our new compiler, save the next piece of code in a file named example_001.cpp:

 1 //Program to test the new C++11 lambda syntax
 2 #include <iostream>
 4 using namespace std;
 6 int main()
 7 {
 8     cout << [](int m, int n) { return m + n;} (2,4) << endl;
 9     return 0;
10 }

Here is the result of building and running the above code on my machine:

1 clang++ -std=c++11 -stdlib=libc++ example_001.cpp -o example_001
2 ./example_001
3 6

Obviously, you don’t need Clang and libc++ to compile a C++11 lambda, GCC will work just fine. Let’s try something that, at the time of this writing, it is not possible to compile with GCC:

 1 //Program to test the new C++11 regular expressions syntax
 2 #include <iostream>
 3 #include <regex>
 4 #include <string>
 6 using namespace std;
 8 int main()
 9 {
10     string input;
11     regex rr("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?");
12     //As long as the input is correct ask for another number
13     while(true)
14     {
15         cout<<"Give me a real number!"<<endl;
16         cin>>input;
17         if(!cin) break;
18         //Exit when the user inputs q
19         if(input=="q")
20             break;
21         if(regex_match(input,rr))
22             cout<<"float"<<endl;
23         else
24         {
25             cout<<"Invalid input"<<endl;
26         }
27     }
28 }

If you are interested in learning more about the new C++11 syntax I would recommend reading Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper 2nd edition:

or, if you are a C++ beginner you could read C++ Primer (5th Edition) by S. B. Lippman, J. Lajoie, B. E. Moo.

Show Comments