How to Build libcxx and libcxxabi by clang on Cent

2019-01-16 17:29发布

问题:

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

回答1:

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/