clang++ for centos

1 cmake 3.8.1

  

  ./configure --prefix=/usr/local/cmake

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




  1. # 检出源代码
  2. cd LLVM     # llvm 的安放目录 LLVM,当然可以换成其他的
  3. svn co http: //llvm.org /svn /llvm-project /llvm /trunk llvm     # llvm 源代码在 LLVM/llvm 下
  4.  
  5. cd llvm /tools     # 切换到 LLVM/llvm/tools 下
  6. svn co http: //llvm.org /svn /llvm-project /cfe /trunk clang     # clang 源代码在 LLVM/llvm/tools 下
  7.  
  8. cd .. /projects     # 切换到 LLVM/llvm/projects 目录下
  9. svn co http: //llvm.org /svn /llvm-project /compiler-rt /trunk compiler-rt     # compiler-rt 在 LLVM/llvm/projects
  10. svn co http: //llvm.org /svn /llvm-project /test-suite /trunk test-suite     # 这一步是可选的
  11.  
  12. # 配置
  13. cd .. /..      # 切换到 LLVM 下
  14. mkdir build     # 创建一个编译目录,以免混淆源文件
  15. cd build
  16.  
  17. $ .. /llvm /configure  --prefix= /usr /local /llvm  --enable-optimized  --enable-targets=host-only     # 安装文件放在 /usr/local/llvm 下;优化+只编译适应本机的代码
  18.  
  19. # 编译和安装
  20. make      # 如果多核的话,可以 make -j4 (4个工作线程)
  21. make  install     # 安装,如果有权限问题请 sudo make install

关于 GLIBCXX 版本过低的问题

如果 llvm+clang 成功编译,但在编译 c++ 程序的时候提示 GLIBCXX 版本过低肿么办?O__O"… GLIBCXX 貌似是 GCC 的库呀,赶脚有些坑爹,O__O"… 看样纸,clang 会使用 libstdc++.so.6 这个库,如果 gcc 版本过低,libstdc++.so.6 实际指向 libstdc++.so.6.0.13,那么么着,需要安装更新的 gcc 。而实际上 me 在 /usr/local/gcc 下已经安装有最新的 GCC,在对应的 lib64 下有 libstdc++.so.6.0.18,所以,me 只需要删除 libstdc++.so.6 这个符号链接就可以,或是将这个符号链接定向到 6.0.18。

查看 libstdc++.so.6.0.13 中的 GLIBCXX 到那个版本的方法:

$ strings /usr/lib64/libstdc++.so.6.0.13 | grep GLIBCXX

me 看到的 libstdc++.so.6.0.13 的 GLIBCXX 到 3.4.13,而 clang 3.4 需要 GLIBCXX 3.4.15 以上,libstdc++.so.6.0.18 的 GLIBCXX 到 3.4.19。

编译过程中出现:/usr/bin/ld: /usr/lib/libcurses.a(lib_setup.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC 问题

以前编译 clang 3.3 (3.4)的时候 me 没有(重)装 ncurses 库,在 make 的时候没有出现问题;后来因为重新装了 ncurses 库,现在装 clang 3.4(3.5) 的时候出现了这个问题。

重装 ncurses 库, 在 make 的时候加上 -fPIC 选项,也就是 $ make CFLAGS='-fPIC' CXXFLAGS='-fPIC'。 简而言之,如果出现类似的问题,可能都需要重装依赖的库,加上编译选项 -fPIC 。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值