【问题标题】:Unable to compile C++17 with clang++ on Mac OSX无法在 Mac OSX 上使用 clang++ 编译 C++17
【发布时间】:2020-07-06 14:55:22
【问题描述】:

clang++ 版本:

Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

尝试一些 C++ 并行性:

#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
#include <random>
#include <math.h>

#define N 10000000

double myFunction(double x) {
    return pow(x, x) / (int(x) % 3);
}

int main() {
    std::random_device rd;
    std::uniform_real_distribution<double> uniform(0.0, 20.0);

    std::vector<double> inputs;
    std::vector<double> returnValues;
    for (int i = 0; i < N; ++i) {
        double r = uniform(rd);
        inputs.push_back(r);
    }

    std::transform(std::execution::par_unseq,
                    inputs.begin(), inputs.end(), 
                    returnValues.begin(), 
                    myFunction);
}

我已尝试使用所有这些进行编译:

$ clang++ -std=c++1z go.cpp -o run
$ clang++ -std=c++17 go.cpp -o run

有和没有#include &lt;optional&gt;。但是都提出了相同的编译器错误:

go.cpp:93:25: error: no member named 'execution' in namespace 'std'; did you mean 'exception'?
    std::transform(std::execution::par_unseq,
                   ~~~~~^~~~~~~~~
                        exception
/Library/Developer/CommandLineTools/usr/include/c++/v1/exception:97:29: note: 'exception' declared here
class _LIBCPP_EXCEPTION_ABI exception
                            ^
go.cpp:93:36: error: no member named 'par_unseq' in 'std::exception'
    std::transform(std::execution::par_unseq,
                   ~~~~~~~~~~~~~~~~^
2 errors generated.
make: *** [parallel] Error 1

编辑:

尝试gcc 也不起作用:

$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ gcc -ltbb -std=c++17 go.cpp -o run
$ gcc -ltbb go.cpp -o run

产生错误:

go.cpp:69:10: fatal error: 'execution' file not found
#include <execution>
         ^~~~~~~~~~~
1 error generated.

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: c++ macos g++ c++17 clang++


    【解决方案1】:

    如果您查看compiler support page on cppreference,您会注意到 Apple Clang 仍然不支持并行算法。

    【讨论】:

    • 好的,我怎样才能最好地编译?
    • 使用 GCC,或者只使用 libstdc++
    • @ZuodianHu 你的意思是仍然通过链接到 libstdc++ 用 clang 编译?
    【解决方案2】:

    你不见了

    #include<algorithm>
    #include<execution>
    

    话虽如此,根据编译器support overview at cppreference.com Apple Clang 尚不支持并行算法扩展。 (在页面上查找“Parallelism TS”。)

    【讨论】:

    • 啊,我明白了。我对 c++ 很陌生,这是否意味着我必须使用不同的编译器?我认为 g++ 是 clang++ 的符号链接。安装新版本的 clang++ 是最明智的吗?还是会弄乱我的苹果系统?我喜欢 clang 中错误消息的质量
    • @lollercoaster 我真的不知道。如果系统上安装了 libstdc++ 和 libtbb,编译器标志 -stdlib=libstdc++ -ltbb 可能足以使其使用 Clang 编译。否则尝试安装 GCC 和 libtbb,那么标志 -ltbb 应该足够了。是的,在 MacOS 上,g++ 是指向clang++ 的链接。所以你有 Clang,但是 Clang (libc++) 的标准库似乎还不支持它。然而,actual GCC (libstdc++) 的标准库可以(与 libtbb 结合使用)。
    • 嗯,这似乎不起作用:gcc -ltbb -std=c++17 go.cpp -o run。编译错误:fatal error: 'execution' file not found #include &lt;execution&gt;
    • @lollercoaster 它应该是g++,而不是gcc(这是用于C,而不是C++)。但是您需要确保 g++实际上是来自 GCC 的 g++,而不是 Clang,或者您至少需要添加 -stdlib=libstdc++ 标志(同样,不能保证这有效) .在您对答案的编辑中,gcc 仍然是指向 clang 的链接(就像 g++ 是指向 clang++ 的链接一样)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2013-08-12
    • 2016-09-20
    • 1970-01-01
    • 2012-12-16
    • 2010-11-10
    • 2022-10-07
    相关资源
    最近更新 更多