本文介绍了如何在CentOS 7中构建C++11构建环境
Clang的定制C++库是libc++(libcxx)。然后,libcxx还需要一个ABI库,libc++abi(libcxxabi)。不幸的是,这两个库有一个循环依赖问题。为了打破循环依赖问题,可以在不连接libc++abi的情况下构建libc++。然后,使用这个libc++,我们可以构建libc++abi链接到libc++。最后,使用libc++abi,我们可以构建一个新的libc++链接到libc++abi。
cmake和clang/llvm安装请见
http://www.cnblogs.com/BinBinStory/p/7461345.html
http://www.cnblogs.com/BinBinStory/p/7499527.html
libc++,libc++abi 构建步骤如下:
1. 获取libc++,libc++abi
2. 下载好了以后,两个都解压出来,得到两个目录:
libcxx-4.0.1.src
libcxxabi-4.0.1.src
3. 第一轮构建libcxx,不需要libcxxabi参与。
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 ..
4. 构建libcxxabi libc++。
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 ../..
5. 第2轮使用libcxxabi构建libcxx。
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
6. 编写一个C++测试程序。
// t.cpp #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; }
7. 通过clang++测试C++编译。
# -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/
翻译自:https://stackoverflow.com/questions/25840088/how-to-build-libcxx-and-libcxxabi-by-clang-on-centos-7