Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Hacking with Clang llvm abi and llvm libc

Niall Douglas edited this page Jul 21, 2014 · 18 revisions

#Hacking with Clang, libc++ and libc++abi

Linux

WARNING: Using libc++ on Linux is an experimental feature. 99% of users will want to use libstdc++ with clang on Linux, you should do that instead unless you have an exceptionally good reason to do otherwise.

This warning does not apply to Apple Mac OS X nor FreeBSD where libc++ is the standard template library for clang. There MaidSafe code automatically uses libc++ with no extra effort.

Ubuntu and Debian

The clang devs have made a ppa available on this page. This will give you a version of clang with lldb as a bonus. These builds are relatively stable and should help us all test clang by using it. The steps are very easy to install clang this way.

sudo wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key|sudo apt-key add -
vim /etc/apt/sources.list (add in the repository links for your distribution from the llvm page above)
sudo apt-get update
sudo apt-get install clang-3.4 clang-3.4-doc libclang-common-3.4-dev libclang-3.4-dev libclang1-3.4 libclang1-3.4-dbg libllvm-3.4-ocaml-dev libllvm3.4 libllvm3.4-dbg lldb-3.4 llvm-3.4 llvm-3.4-dev llvm-3.4-doc llvm-3.4-examples llvm-3.4-runtime clang-format-3.4 

That's clang installed!

libc++ using libstdc++ (gcc version)

To install libc++abi this must be installed first. Essentially in a linux distribution that means this is an essential step, whether you intend to go down the libc++abi (recommended) route or not.

svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
echo | g++ -Wp,-v -x c++ - -fsyntax-only

This will output

ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
### #include "..." search starts here:
### include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.

Note the first two entries happen to be what we are looking for. This may not be correct on other platforms. We can now run CMake:

CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libsupc++ -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/x86_64-linux-gnu/c++/4.7/" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr <libc++-source-dir>
make
sudo make install

You can now run clang with -stdlib=libc++.

Note : if you have multiple version of gcc installed and know the path of where they are, you can skip the "echo" step and jump to the "cmake" step by just specifying where the gcc you want to use is. For example :

CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libsupc++ -DLIBCXX_LIBSUPCXX_INCLUDE_PATHS="/usr/gcc_4_7/include/c++/4.7.3/;/usr/gcc_4_7/include/c++/4.7.3/x86_64-linux-gnu/" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ~/libcxx/

libc++abi

The abi is the interface to the hardware and the standard library libc++ needs an abi to connect to. There are choices but for this project we will use the llvm supplied version. It's heavily worked on and feature complete.

###Install depenedencies

sudo apt-get install g++ subversion cmake swig python-dev libedit-dev libunwind8 libunwind8-dev

###Install libc++abi

svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
cd libcxxabi/lib
./buildit
sudo cp libc++abi.so.1.0 /usr/lib
sudo ln -s /usr/lib/libc++abi.so.1.0 /usr/lib/libc++abi.so.1
sudo ln -s /usr/lib/libc++abi.so.1 /usr/lib/libc++abi.so

You now have and abi installed.

libc++

###install libc++ (using libc++abi) This is recommended for running maidsafe libraries as it ensures a tight integration with libc++ and clang++. They ensure all the correct libraries are used at the abi level and will help with exception pointers and other advanced features that may be required for modern c++ coding.

svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
cd libcxx
mkdir build
cd build
CC=clang CXX=clang++ cmake -G "Unix Makefiles" -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_LIBCXXABI_INCLUDE_PATHS="<libc++abi-source-dir>/include" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr ../
make
sudo make install

Configure project

to configure :

cd Maidsafe (where superproject is checked out to)
mkdir buildclangDebug
cd buildclangDebug
cmake ../ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang -DHAVE_LIBC++=1 -DHAVE_LIBC++ABI=1