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

apt.llvm.org的Clang-15无法编译C++17的std::execution::par特性

问题与解决方案:Clang-15在Ubuntu下无法编译std::execution::par代码

问题概述

已向LLVM缺陷追踪器提交相关问题。在Ubuntu 22.04系统中,使用apt.llvm.org提供的Clang-15,且已安装libtbb2-dev、clang-15-openmp等潜在PSTL后端依赖,但编译使用C++17特性std::execution::par的代码时失败。

复现步骤

以下Dockerfile可复现该问题:

FROM ubuntu:22.04

RUN apt-get update && apt-get -y install \
    software-properties-common gpg wget \
    make libssl-dev

RUN apt-get install -y build-essential libthrust-dev libtbb2-dev libtbb2

# 安装Clang-15
RUN wget https://apt.llvm.org/llvm.sh && \
    chmod +x llvm.sh && \
    ./llvm.sh 15 all
ENV PATH=$PATH:/usr/lib/llvm-15/bin

RUN echo "\
\n#include <execution> \n \
\n#include <algorithm> \n \
\n#include <vector> \n \
int main(void) { \n \
    std::vector<float> images = {1,2,3,4,5}; \n \
    std::vector<float> results; \n \
    results.resize(images.size()); \n \
    std::transform(std::execution::par, images.begin(), images.end(), results.begin(), [](const auto &input){ return input; }); \n \
}" > hello.cpp

RUN clang -std=c++17 -stdlib=libc++ hello.cpp -o hello && ./hello

错误信息

执行后出现以下编译错误:

hello.cpp:11:26: error: no member named 'execution' in namespace 'std'; did you mean 'exception'?
     std::transform(std::execution::par, images.begin(), images.end(), results.begin(), [](const auto &input){ return input; }); 
                    ~~~~~^~~~~~~~~
                         exception
/usr/lib/llvm-15/bin/../include/c++/v1/exception:100:29: note: 'exception' declared here
class _LIBCPP_EXCEPTION_ABI exception
                            ^
hello.cpp:11:37: error: no member named 'par' in 'std::exception'
     std::transform(std::execution::par, images.begin(), images.end(), results.begin(), [](const auto &input){ return input; }); 
                    ~~~~~~~~~~~~~~~~^
2 errors generated.
The command '/bin/sh -c clang -std=c++17 -stdlib=libc++ hello.cpp -o hello && ./hello' returned a non-zero code: 1

原因与解决方案

核心原因

出现该错误是因为:

  1. libc++中<execution>头文件的内容受_LIBCPP_HAS_PARALLEL_ALGORITHMS宏控制,若未定义此宏,std::execution命名空间不会被暴露。
  2. 编译时未链接并行后端库(如TBB),且未显式启用并行算法支持。

修复步骤

修改Dockerfile中的编译命令,添加必要的宏定义与链接参数:

RUN clang -std=c++17 -stdlib=libc++ -D_LIBCPP_HAS_PARALLEL_ALGORITHMS -ltbb hello.cpp -o hello && ./hello

额外说明

若上述修改后仍无法正常运行,可能是apt.llvm.org提供的Clang-15包未默认启用PSTL支持。此时需自行编译LLVM栈,并在编译libc++时开启LIBCXX_ENABLE_PARALLEL_ALGORITHMS与LIBCXX_PARALLEL_ALGORITHMS_BACKEND=TBB选项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 12:55:25