如何在 CentOS 7 上通过 clang 构建 libcxx 和 libcxxabi

2022-01-13 00:00:00 clang centos c++ c++11 libc++

我想在 CentOS 7 上使用带有 clang/clang++ 的 C++11 或 C++14.如何构建这个构建环境?

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

推荐答案

本文讲授如何在 CentOS 7 上搭建 C++11 构建环境:RHEL 的 EPEL repo 提供 Clang 包,但没有 C++ 库包.所以,这些部分手工制作有点麻烦.为 Clang 定制的 C++ 库是 libc++ (libcxx) [1].然后,libcxx 还需要一个 ABI 库,libc++abi (libcxxabi) [2].不幸的是,这两个库存在循环依赖问题.为了打破循环依赖问题,可以不链接 libc++abi 构建 libc++.然后,有了这个 libc++,我们就可以构建链接 libc++ 的 libc++abi.最后,通过libc++abi,我们可以构建一个新的libc++链接libc++abi.

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.

clang、libc++、libc++abi环境搭建步骤如下:

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

  1. 添加 RHEL 的 EPEL 存储库.打开以下链接并找到如何使用这些额外的软件包?"部分https://fedoraproject.org/wiki/EPEL
    找到你的 CentOS 版本的 epel 包.例如:

  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

  • 安装 Subversion 以获得最新的 libcxx 和 libcxxabi.

  • Install Subversion for getting the latest libcxx and libcxxabi.

    sudo yum install svn
    

  • 安装 Clang 和 llvm-devel(使用 llvm-config).

  • Install Clang and llvm-devel (with llvm-config).

    sudo yum install clang llvm-devel
    

  • 安装 cmake.

  • 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.
    

  • 在没有 libcxxabi 的情况下构建 libcxx 的第一轮.

  • 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 ..
    

  • 使用 libc++ 构建 libcxxabi.

  • 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 ../..
    

  • 第二轮用 libcxxabi 构建 libcxx.

  • 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
    

  • 编写一个 C++ 测试程序.

  • Write a C++ test program.

    // t.cpp
    #include <iostream>
    using namespace std;
    int main() {
      cout << "Hello world!" << endl;
    }
    

  • 通过clang++测试C++编译.

  • 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
    

  • 参考:
    [1] http://libcxx.llvm.org/
    [2] http://libcxxabi.llvm.org/

    相关文章