You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编译配置Clang以使用libc++和PSTL?编译报错求助

问题:Clang搭配libc++编译PSTL代码失败

背景与复现

在Ubuntu 23.10系统中,以下代码使用libstdc++时,通过GCC或Clang均可正常编译运行:
编译命令:g++ execution.cpp -ltbb 或 clang++ execution.cpp -ltbb

代码示例:

#include <vector>
#include <execution>
#include <cassert>

int main(int argc, char *argv[])
{
  using namespace std;

  vector v{1,2,3,4,5,6,7,8};
  for_each(std::execution::seq,       v.begin(), v.end(), [](auto& x) { x++; });
  for_each(std::execution::par,       v.begin(), v.end(), [](auto& x) { x++; });
  for_each(std::execution::par_unseq, v.begin(), v.end(), [](auto& x) { x++; });
  for_each(std::execution::seq,       v.begin(), v.end(), [](auto& x) { x++; });
  assert((v==vector{5,6,7,8,9,10,11,12}));
  return 0;
}

但使用Clang搭配libc++编译时失败,执行命令:clang++ -stdlib=libc++ execution.cpp -ltbb,报错信息如下:

error: no member named 'execution' in namespace 'std'; did you mean 'exception'?

尝试的解决操作

参考相关资料后,我通过以下CMake命令编译启用了PSTL的LLVM/Clang:

cmake ../llvm \
-DLLVM_USE_LINKER=gold \
-DCMAKE_INSTALL_PREFIX=$PWD/../install_pstl \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
-DLLVM_ENABLE_PROJECTS="clang;pstl" \
-DPSTL_PARALLEL_BACKEND=tbb

随后使用如下命令编译目标程序:

clang++ -std=c++23 -stdlib=libc++ -Wl,-rpath,"$HOME/llvm-project-main/install_pstl/lib:$HOME/llvm-project-main/install_pstl/lib/x86_64-unknown-linux-gnu:$LD_LIBRARY_PATH" execution.cpp

当前遇到的问题

此时编译仍未成功:虽然能找到std::execution命名空间,但无法识别seq、par、unseq、par_unseq这4个常量,报错信息为:

error: no member named 'seq' in namespace 'std::execution'

我的CMake配置是否存在错误?编译命令需要调整吗?此前有相关LLVM bug讨论但已关闭。


内容的提问来源于stack exchange,提问作者user2023370

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 15:17:13