Solarian Programmer

My programming ramblings

Building Clang 7, libc++ and lldb on Ubuntu 18.04

Posted on January 17, 2013 by Sol

Updated on 26 September 2018

Clang with libc++ represents today a viable alternative to GCC for C and C++ programmers. 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 18.04, but it should work on Debian and WSL without a problem.

Let’s start by updating your Linux system, open a Terminal and paste the next two lines:

1 sudo apt update
2 sudo apt upgrade

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

1 sudo apt install build-essential subversion cmake python3-dev libncurses5-dev libxml2-dev libedit-dev swig doxygen graphviz xz-utils

Next step, is to get the latest stable versions of Clang, LLVM, libc++ and a few other utilities:

 1 cd ~
 2 mkdir llvm_all && cd llvm_all
 3 svn co http://llvm.org/svn/llvm-project/llvm/tags/RELEASE_700/final llvm
 4 
 5 cd llvm/tools
 6 svn co http://llvm.org/svn/llvm-project/cfe/tags/RELEASE_700/final clang
 7 
 8 cd llvm/projects
 9 svn co http://llvm.org/svn/llvm-project/compiler-rt/tags/RELEASE_700/final compiler-rt
10 svn co http://llvm.org/svn/llvm-project/libcxx/tags/RELEASE_700/final libcxx
11 svn co http://llvm.org/svn/llvm-project/libcxxabi/tags/RELEASE_700/final libcxxabi
12 svn co http://llvm.org/svn/llvm-project/polly/tags/RELEASE_700/final polly
13 svn co http://llvm.org/svn/llvm-project/lld/tags/RELEASE_700/final lld
14 svn co http://llvm.org/svn/llvm-project/openmp/tags/RELEASE_700/final openmp
15 svn co http://llvm.org/svn/llvm-project/libunwind/tags/RELEASE_700/final libunwind

Now, we can build all the above, depending on the speed of your computer, this could take from 30 minutes to a few hours:

1 cd ~/llvm_all
2 mkdir build && cd build
3 cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_BUILD_DOCS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/clang_7.0.0 ../llvm
4 
5 make -j 8
6 sudo make install/strip

In order to be able to use Clang we’ll need to add it to our system path, you can do this with:

1 cd ~
2 echo 'export PATH=/usr/local/clang_7.0.0/bin:$PATH' >> ~/.bashrc
3 echo 'export LD_LIBRARY_PATH=/usr/local/clang_7.0.0/lib:LD_LIBRARY_PATH' >> ~/.bashrc
4 source ~/.bashrc

Now, let’s download and build lldb:

1 cd ~/llvm_all
2 svn co http://llvm.org/svn/llvm-project/lldb/tags/RELEASE_700/final lldb
3 mkdir build2 && cd build2
4 cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local/clang_7.0.0 ../llvm
5 make -j 4
6 sudo make install/strip

Next, let’s try to compile a program that uses the C++17 Filesystem:

1 #include <iostream>
2 #include <filesystem>
3 
4 int main() {
5     for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
6         std::cout << file.path() << '\n';
7     }
8 }

Save the above file as test_fs.cpp and compile it with:

1 clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++fs

If you run the resulting executable, you should see a list with all files from the working directory. This is what I see on my machine:

1 ~/DEV $ clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++fs
2 ~/DEV $ ./test_fs
3 "./test_fs"
4 "./if_test"
5 "./if_test.cpp"
6 "./test_fs.cpp"
7 ~/DEV $

If you want to link your code with the OpenMP library, use the:

1 -fopenmp=libiomp5

flag, e.g.:

1 clang++ -std=c++17 -Wall -pedantic -stdlib=libc++ your_openmp_cpp_code.cpp -fopenmp=libiomp5

If you are interested to learn more about modern C++ I would recommend reading A tour of C++ by Bjarne Stroustrup.

or Effective Modern C++ by Scott Meyers.


Show Comments