gpt4 book ai didi

c++20 - 缺少 Apple Clang 13 C++20 模块支持

转载 作者:行者123 更新时间:2023-12-05 01:05:53 26 4
gpt4 key购买 nike

根据 official documentation , Clang 13 通过使用 -fmodules 命令行参数支持 C++20 模块。

我什至无法在基于 Intel 或 M1 的 mac 上使用 Clang 13 (macOS Monterey) 编译基本模块。

假设文件 module.cpp 的文本内容如下:

export module a;

export int f(int a, int b) {
return a + b;
}

运行以下内容:

$ clang++ --version
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: x86_64-apple-darwin21.1.0

$ clang++ -std=c++20 -stdlib=libc++ -fmodules -fbuiltin-module-map -c module.cpp
module.cpp:1:8: error: expected template
export module a;
^
module.cpp:1:8: error: unknown type name 'module'
module.cpp:3:8: error: expected template
export int f(int a, int b) {
^
3 errors generated.

在 ARM M1 芯片上测试结果相同:

$ clang++ --version
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: arm64-apple-darwin21.1.0

是否有其他方法让模块正常工作,或者 Apple Clang 13 是否存在一些未记录的 C++20 模块限制?

注意:使用实验性 -fmodules-ts 标志进行编译。

最佳答案

原因

感谢您的评论 - 上面的错误表明 Clang 版本是在没有模块支持的情况下构建的。这是 Xcode 附带的,即通过在终端中运行 xcode-select --install

解决方案

正如所建议的那样,解决方案是通过 HomeBrew 安装 Clang,操作如下(在 macOS Monterey 上测试):

brew install llvm

Clang 安装到 /opt/homebrew/opt/llvm/bin/clang++。确认运行版本如下图:

% /opt/homebrew/opt/llvm/bin/clang++ --version
Homebrew clang version 13.0.0
Target: arm64-apple-darwin21.1.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

这与 Xcode 系统范围的默认版本不同:

% clang++ --version
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: arm64-apple-darwin21.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

工作示例

根据 repo 查看工作示例的步骤由@alexpanter 发布:

main.cpp

import <iostream>;
import mathlib;

using namespace std;

int main() {
cout << "Modules, baby!" << endl;
cout << "2 plus 3 makes " << add(2, 3) << " says module 'mathlib'" << endl;
}

数学库.cpp

export module mathlib;

export int add(int a, int b)
{
return a + b;
}

通过在与上述文件相同的目录中的终端中运行来构建:

/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -c -Xclang -emit-module-interface mathlib.cpp -o mathlib.pcm
/opt/homebrew/opt/llvm/bin/clang++ -std=c++20 -fmodules -c -fprebuilt-module-path=. main.cpp -o main.o
/opt/homebrew/opt/llvm/bin/clang++ -std=c++2a -fmodules -o main main.o *.pcm

测试基于模块的可执行文件:

./main

预期输出:

Modules, baby!
2 plus 3 makes 5 says module 'mathlib'

关于c++20 - 缺少 Apple Clang 13 C++20 模块支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70168181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com