【问题标题】:Apple Clang 13 C++20 Module Support Missing缺少 Apple Clang 13 C++20 模块支持
【发布时间】:2022-01-07 02:22:11
【问题描述】:

根据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 标志进行编译是可行的。

【问题讨论】:

  • homebrew 的 clang 13 与 -fmodules 配合使用,在 m1 mac 上测试。
  • 注意:不幸的是,apple clang 和 mainline clang 是不同的东西。
  • @Mgetz 感谢您的确认 - 我确实在运行 Xcode 版本 - HomeBrew Clang 似乎与官方文档相符
  • @VainMan - 感谢您抽出宝贵时间 - 这是 Xcode 与 Homebrew Clang 的问题

标签: c++20 clang++ apple-m1 c++-modules macos-monterey


【解决方案1】:

原因

感谢 cmets - 上面的错误表明 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

工作示例

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

ma​​in.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;
}

ma​​thlib.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'

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2021-10-03
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多