如何通过 CMake 将 clang 配置为使用 clang 版本的标准库?[复制]

How do I configure clang to use clang version of the standard library via CMake? [duplicate]

提问人:Elad Maimoni 提问时间:11/16/2023 最后编辑:Elad Maimoni 更新时间:11/16/2023 访问量:43

问:

我正在使用 clang 16.0.4 编译一个大型 C++ 项目。

我有一个 docker 镜像,可以设置一个干净的开发环境。当我在该图像上编译代码时,一切都很顺利。我认为这是因为 clang 正在使用自己的 C++ 标准库实现。

但是,在某些环境中,我目睹了 clang 正在拾取可能安装在系统上的 gcc 相关位的情况。例如,下面是一个典型的故障:

  /usr/bin/clang++  -I/home/runner/work/cranebit/cranebit -I/home/runner/work/cranebit/cranebit/src/3rd/concurrencpp -fPIC   -fPIC -O2 -g -DNDEBUG -std=gnu++2b -fPIC -Wall -Wextra -Wshadow -Wformat=2 -Wunused -Werror -std=c++2b -MD -MT src/3rd/concurrencpp/CMakeFiles/concurrencpp.dir/source/task.cpp.o -MF src/3rd/concurrencpp/CMakeFiles/concurrencpp.dir/source/task.cpp.o.d -o src/3rd/concurrencpp/CMakeFiles/concurrencpp.dir/source/task.cpp.o -c /home/runner/work/cranebit/cranebit/src/3rd/concurrencpp/source/task.cpp
  In file included from /home/runner/work/cranebit/cranebit/src/3rd/concurrencpp/source/task.cpp:2:
  In file included from /home/runner/work/cranebit/cranebit/src/3rd/concurrencpp/concurrencpp/results/impl/consumer_context.h:5:
  In file included from /home/runner/work/cranebit/cranebit/src/3rd/concurrencpp/concurrencpp/results/result_fwd_declarations.h:7:
  In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/memory:69:
  In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_uninitialized.h:63:
  In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_algobase.h:67:
  Error: /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_iterator.h:2618:35: error: missing 'typename' prior to dependent type name 'iterator_traits<_It>::iterator_category'
        { using iterator_category = iterator_traits<_It>::iterator_category; };

请注意标准库的 gcc 实现中的文件,以及以某种方式传递给编译器的 -std=gnu++2b 标志。

我会配置 cmake,这样就不会拿起 gcc 相关的工具和库。但我不确定实现这一目标的正确和完整的方法是什么。

具体说来:

  • 我应该设置哪些确切的标志(std=c++2b? -stdlib=libc++?)
  • 是否应该按目标设定?通过工具链文件?
  • 如果我将它们设置为 ,我应该附加到此列表吗?覆盖它?在哪些阶段?CMAKE_CXX_FLAGS
  • 如果系统上安装了多个版本的 Clang 库,该怎么办?我怎样才能选择合适的?

目前,我使用一个 cmake 工具链文件来配置 clang:

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

如果我手动设置标志,那么我有时会收到来自旧版本的 C++ 库的错误。例如:-stdlib=libc++ -std=c++2b

Error: /home/runner/work/cranebit/cranebit/src/3rd/concurrencpp/concurrencpp/executors/executor.h:87:29: error: no viable conversion from 'std::vector<task>' to 'std::span<task>'
              std::span<task> span = tasks;
                              ^      ~~~~~
  /usr/lib/llvm-14/bin/../include/c++/v1/span:401:15: note: candidate constructor not viable: no known conversion from 'std::vector<task>' to 'const std::span<concurrencpp::task, 18446744073709551615> &' for 1st argument
      constexpr span           (const span&) noexcept = default;

因此,我需要一种方法来一致地控制 clang 使用的 C++ 库实现。

C cmake clang clang++

评论


答: 暂无答案