【问题标题】:How to build clang with libc++ as default via LLVMs build_docker_image.sh?如何通过 LLVM build_docker_image.sh 默认使用 libc++ 构建 clang?
【发布时间】:2019-01-13 17:42:00
【问题描述】:

我想用 clang 6 和 libc++ 作为默认 C++ 标准库构建一个 docker 容器。我想包含(并使用)LLVM 项目 clang、libcxx、libcxxabi、lld 和 compiler-rt。

为了将 libc++ 设置为默认值,我使用了 CMAKE 选项 CLANG_DEFAULT_CXX_STDLIB=libc++(另外还使用了 DCLANG_DEFAULT_RTLIB=compiler-rt 默认为 compiler-rt)。第一阶段构建确实有效,但似乎没有安装 libc++:

$ git clone https://github.com/llvm-mirror/llvm.git
$ cd llvm
$ git checkout release_60
$ utils/docker/build_docker_image.sh -s debian8 \
    -d "clang" -t "6" --branch branches/release_60 \
    -p compiler-rt -p libcxxabi -p libcxx -p clang \
    -i stage2-install-compiler-rt \
    -i stage2-install-libcxxabi \
    -i stage2-install-libcxx \
    -i stage2-install-clang \
    -i stage2-install-clang-headers -- \
    -DCMAKE_BUILD_TYPE=Release \
    -DCLANG_DEFAULT_CXX_STDLIB=libc++ \
    -DCLANG_DEFAULT_RTLIB=compiler-rt \
    -DLIBCXXABI_USE_LLVM_UNWINDER=YES \
    -DCLANG_ENABLE_BOOTSTRAP=ON \
    -DBOOTSTRAP_CMAKE_BUILD_TYPE=Release \
    -DBOOTSTRAP_CLANG_DEFAULT_CXX_STDLIB=libc++ \
    -DBOOTSTRAP_CLANG_DEFAULT_RTLIB=compiler-rt \
    -DBOOTSTRAP_LIBCXX_USE_COMPILER_RT=YES \
    -DBOOTSTRAP_LIBCXXABI_USE_COMPILER_RT=YES \
    -DCLANG_BOOTSTRAP_TARGETS="install-compiler-rt;install-libcxxabi;install-libcxx;install-clang;install-clang-headers"

在第二阶段构建我得到:

[...]
-- The C compiler identification is Clang 6.0.1
-- The CXX compiler identification is Clang 6.0.1
-- The ASM compiler identification is unknown
-- Found assembler: /tmp/clang-build/build/./bin/clang
-- Check for working C compiler: /tmp/clang-build/build/./bin/clang
-- Check for working C compiler: /tmp/clang-build/build/./bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /tmp/clang-build/build/./bin/clang++
-- Check for working CXX compiler: /tmp/clang-build/build/./bin/clang++ -- broken
CMake Error at /usr/local/share/cmake-3.7/Modules/CMakeTestCXXCompiler.cmake:44 (message):
  The C++ compiler "/tmp/clang-build/build/./bin/clang++" is not able to
  compile a simple test program.

  It fails with the following output:

   Change Dir: /tmp/clang-build/build/tools/clang/stage2-bins/CMakeFiles/CMakeTmp



  Run Build Command:"/usr/bin/ninja" "cmTC_d8e88"

  [1/2] Building CXX object CMakeFiles/cmTC_d8e88.dir/testCXXCompiler.cxx.o

  [2/2] Linking CXX executable cmTC_d8e88

  FAILED: : && /tmp/clang-build/build/./bin/clang++
  CMakeFiles/cmTC_d8e88.dir/testCXXCompiler.cxx.o -o cmTC_d8e88 && :

  /usr/bin/ld: cannot find -lc++

  clang-6.0: error: linker command failed with exit code 1 (use -v to see
  invocation)

  ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:45 (project)
[...]

单阶段构建似乎已成功完成:

$ git clone https://github.com/llvm-mirror/llvm.git
$ cd llvm
$ git checkout release_60
$ utils/docker/build_docker_image.sh -s debian8 \
    -d "clang-1stage" -t "6" --branch branches/release_60 \
    -p compiler-rt -p libcxxabi -p libcxx -p clang \
    -i install-compiler-rt \
    -i install-libcxxabi \
    -i install-libcxx \
    -i install-clang \
    -i install-clang-headers -- \
    -DCMAKE_BUILD_TYPE=Release \
    -DCLANG_DEFAULT_CXX_STDLIB=libc++ \
    -DCLANG_DEFAULT_RTLIB=compiler-rt

但是当我运行容器时,链接也有问题:

$ docker run -it clang:6
root@1e95a7ffe57a:/# echo "#include <iostream>" > main.cpp
root@1e95a7ffe57a:/# echo "int main(){ std::cout << \"Hello World\\n\"; }" >> main.cpp
root@1e95a7ffe57a:/# clang++ main.cpp 
/usr/bin/ld: /tmp/main-7d03e7.o: undefined reference to symbol '_Unwind_Resume@@GCC_3.0'
//lib/x86_64-linux-gnu/libgcc_s.so.1: error adding symbols: DSO missing from command line
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

我做错了什么?

【问题讨论】:

    标签: docker clang llvm libc++


    【解决方案1】:

    您缺少另一个 LLVM 项目,libunwind

    LLVM 的 libunwind 提供轻量级堆栈展开接口,并与 libcxx 和 libcxxabi 紧密耦合。

    您应该将 libunwind 检出到 projects 文件夹中,它应该由 LLVM 自动选择。您可能还需要在 CMake 命令行中添加 -DLIBCXXABI_USE_LLVM_UNWINDER=YES

    有用的链接:Clang 8 documentation - Assembling a Complete Toolchain

    【讨论】:

    • 这解决了一个阶段的构建问题并为我提供了一个工作设置——谢谢!两阶段构建仍然得到相同的错误。可能是构建脚本中的错误?
    • @BenjaminBuch 肯定 :-) 你的意思是,第二阶段失败错误消息与第一阶段相同 - 未定义对 _Unwind_Resume 的引用?尝试从第二阶段检查 CMakeOutput.logCMakeError.log - 它们更明确地指出失败的原因
    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多