Apple Clang 13 C++20 模块支持缺失

问题描述 投票:0回答:1

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

c++ c++20 clang++ macos-monterey c++-modules
1个回答
11
投票

原因

感谢您的评论 - 上面的错误表明 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 松散地查看工作示例的步骤:

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;
}

mathlib.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'
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.