How to Build libcxx and libcxxabi by clang on CentOS 7

15,378

This article teaches how to build C++11 building environment on CentOS 7: RHEL's EPEL repo provides Clang packages, but no C++ library packages. So, these parts are a bit troublesome to be built by hand. The customized C++ libraries for Clang is libc++ (libcxx) [1]. Then, libcxx also needs an ABI library, libc++abi (libcxxabi) [2]. Unfortunately, these two libraries have a circular dependency problem. For breaking the circular dependency problem, libc++ can be built without linking libc++abi. Then, with this libc++, we can build libc++abi linking libc++. Finally, with the libc++abi, we can build a new libc++ linking libc++abi.

The clang, libc++, and libc++abi environment building steps are given in the following:

  1. Add RHEL's EPEL repo. Open the following link and find the section "How can I use these extra packages?" https://fedoraproject.org/wiki/EPEL
    Find the epel package for your CentOS version. E.g.,:

    sudo rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    
  2. Install Subversion for getting the latest libcxx and libcxxabi.

    sudo yum install svn
    
  3. Install Clang and llvm-devel (with llvm-config).

    sudo yum install clang llvm-devel
    
  4. Install cmake.

    cd /usr/local
    wget https://cmake.org/files/v3.5/cmake-3.5.2-Linux-i386.sh
    sudo chmod 755 cmake-3.5.2-Linux-i386.sh
    sudo ./cmake-3.5.2-Linux-i386.sh
    # Check cmake is in /usr/local/bin.
    
  5. 1st round to build libcxx without libcxxabi.

    # Get libcxx.
    svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
    cd libcxx
    # It is not recommended to build libcxx in the source root directory.
    # So, we make a tmp directory.
    mkdir tmp
    cd tmp
    # Specifying CMAKE_BUILD_TYPE to Release shall generate performance optimized code.
    # The CMAKE_INSTALL_PREFIX changes the install path from the default /usr/local to /usr.
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ ..
    sudo make install
    cd ..
    rm tmp -rf
    cd ..
    
  6. Build libcxxabi with libc++.

    # Get libcxxabi.
    svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
    cd libcxxabi
    mkdir tmp
    cd tmp
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBCXXABI_LIBCXX_INCLUDES=../../libcxx/include ..
    sudo make install
    cd ../..
    
  7. 2nd round to build libcxx with libcxxabi.

    cd libcxx
    mkdir tmp
    cd tmp
    # This time, we want to compile libcxx with libcxxabi, so we have to specify LIBCXX_CXX_ABI=libcxxabi and the path to libcxxabi headers, LIBCXX_LIBCXXABI_INCLUDE_PATHS.
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_CXX_ABI_INCLUDE_PATHS=../../libcxxabi/include ..
    sudo make install
    
  8. Write a C++ test program.

    // t.cpp
    #include <iostream>
    using namespace std;
    int main() {
      cout << "Hello world!" << endl;
    }
    
  9. Test C++ compilation by clang++.

    # -std specifies the C++ standard. -stdlib specifies the C++ library you want to use with clang/clang++. -lc++abi is necessary, because the new LD (linker and loader) on CentOS 7 doesn't allow indirect library linking.
    clang++ -std=c++11 -stdlib=libc++ -lc++abi t.cpp
    ./a.out
    

References:
[1] http://libcxx.llvm.org/
[2] http://libcxxabi.llvm.org/

Share:
15,378

Related videos on Youtube

Meng-Yuan Huang
Author by

Meng-Yuan Huang

Updated on October 17, 2020

Comments

  • Meng-Yuan Huang
    Meng-Yuan Huang over 3 years

    I want to use C++11 or C++14 with clang/clang++ on CentOS 7. How do I build this building environment?

    • phoenix
      phoenix about 7 years
      If you don't want to build anything, you can use C++11 (without <regex>) with Clang 3.4.2 and the pre-installed libstdc++ on CentOS 7. Enable the EPEL repository and yum install clang to get Clang 3.4.2.
    • phoenix
      phoenix about 6 years
      See this answer for how to install the latest Clang on CentOS 7 using the SCL repository.
  • Steven Lu
    Steven Lu over 9 years
    This is really helpful, I wish you could share a more in-depth explanation of why these steps are necessary.
  • Meng-Yuan Huang
    Meng-Yuan Huang over 9 years
    Sorry, I am not a professional to explain everything exactly. However, these steps just work on CentOS 7. I will try to add some comments as possible as I can.
  • Sardathrion - against SE abuse
    Sardathrion - against SE abuse over 9 years
    @StevenLu: Centos 7 is based off Fedora 19. Therefore there is little chance of the same steps working. This is expected behaviour.
  • Alex
    Alex almost 7 years
    libcxx-3.8.0-3.el7.x86_64 is available through epel, one can yum install libcxx