Compiling llvm, clang and libc++ on Linux
Posted on October 16, 2011 by Sol

There is an update of this tutorial for at http://solarianprogrammer.com/2013/01/17/building-clang-libcpp-ubuntu-linux/.

Last week I've started a series of tutorials about C++11, the first entry in this series was about regular expressions in C++11. On my MacBook Pro with Lion and clang++-3.0 I was able to compile with success all the examples presented in the tutorial, however when I've tried to use more "advanced" concepts like raw string literals that can simplify the use of regular expression (no need for escaping special characters) I've noticed that Apple's version of clang and libc++ doesn't include raw string literals.

The solution was obviously to compile the last versions of llvm, clang and libc++ from sources. Because I use my computer to hack some Objective-C code I didn't want to mess my system installing new versions of llvm and clang so I've tried to install these on a test Linux box. After a few hours of struggle I was able to compile C++11 code that uses regular expressions and raw strings literals on Linux! This was interesting enough to deserve a separate post on my blog and hopefully will help others to get started with clang++ and libc++ on their Linux boxes.

I have a dedicated machine for running Linux (Ubuntu 11.10 64bits), but you could follow the instructions from this tutorial in a virtual machine if you don't use usually Linux but you want to learn C++11.

If you don't already have gcc, g++ and subversion installed you can add them to your system in a matter of minutes, fire up a Terminal and paste these lines, one by one, writing Y when you will be asked if you want to proceed with the installation:

1
2
3
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install subversion

Next step is to get the last version of llvm. For my installation I've downloaded llvm in my home folder, if you prefer to save this in a different place create a directory where you want to save the files and cd to this directory. Getting the code is simple:

1
2
3
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

Now, we need to edit some hard coded paths from clang, without this clang won't be able to find your C++ header files:

1
2
cd clang/lib/Frontend
gedit InitHeaderSearch.cpp &

The above will open InitHeaderSearch.cpp in gedit, search for the line that starts with "FIXME: temporary hack: hard-coded paths." at about line 433 comment the first AddPath line and add the next 8 lines:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
        // FIXME: temporary hack: hard-coded paths.
        //AddPath("/usr/local/include", System, true, false, false);

        AddPath("/usr/include/c++/4.6", System, true, false, false);
        AddPath("/usr/include/c++/4.6/x86_64-linux-gnu/.", System, true, false, false);
        AddPath("/usr/include/c++/4.6/backward", System, true, false, false);
        AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.6.1/include", System, true, false, false);
        AddPath("/usr/local/include", System, true, false, false);
        AddPath("/usr/lib/gcc/x86_64-linux-gnu/4.6.1/include-fixed", System, true, false, false);
        AddPath("/usr/include/x86_64-linux-gnu", System, true, false, false);
        AddPath("/usr/include", System, true, false, false);

      break;

Save the file and close gedit, next we will create a build directory and we will configure the code for compilation:

1
2
3
4
cd ~/llvm
mkdir build
cd build
../configure --enable-optimized --enable-targets=host-only

Next we will compile the code in parallel, I have a quad core processor so I could safely use eight threads, you could safely use the next line even if you have a single core machine:

1
2
make -j 8
sudo make install

Congrats now you have llvm and clang installed on your Linux box, however this installation will use the default libstc++ from Linux which has no support for regular expressions. We need to install a new implementation of the Standard Template Library from llvm (this is not installed by default with llvm and clang at the time of this writing).

We will start by getting the source code for libc++ from llvm, and compiling the library:

1
2
3
cd ~
svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
cd libcxx/lib

Update Dec 19, 2011: If you plan to do multithreading programming with clang++ in Linux, you should enable manually enable the atomic header. Open ~/libcxx/include/atomic in gedit and remove "!" from the highlighted line:

1
2
3
#if !__has_feature(cxx_atomic)
#error <atomic> is not implemented
#else

You could compile libcxx with:

1
./buildit

At the end of this stage you should have a compiled libc++ in the libcxx/lib folder, the name of the file is libc++.so.1.0. In my case I have libcxx in my home folder: /home/sol/libcxx for you (if you have followed my procedure) it should be something like /home/user_name/libcxx .

Next step is to create symbolic links in order for the compiler to be able to find our new library:

1
2
3
4
5
cd /usr/lib
sudo ln -sf /home/sol/libcxx/lib/libc++.so.1.0 libc++.so 
sudo ln -sf /home/sol/libcxx/lib/libc++.so.1.0 libc++.so.1
cd /usr/include/c++
sudo ln -sf /home/sol/libcxx/include v1

Now, you really have a complete system: llvm, clang and libc++.Let's try to run a C++11 regex example that won't compile with the default g++ from your system (the code is taken from my regular expression tutorial):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example 3 - real number test 
#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main()
{
	string input;
	regex rr("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?");
	//As long as the input is correct ask for another number
	while(true)
	{
		cout<<"Give me a real number!"<<endl;
		cin>>input;
		//Exit when the user inputs q
		if(input=="q")
			break;
		if(regex_match(input,rr))
			cout<<"float"<<endl;
		else
		{
			cout<<"Invalid input"<<endl;
		}
	}
}

You could compile the above code (supposing it is saved under the name regex_03.cpp in your home folder):

1
2
cd ~
clang++ -std=c++0x -stdlib=libc++ regex_03.cpp -o regex_03.out

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.

blog comments powered by Disqus