【发布时间】:2021-07-16 12:27:33
【问题描述】:
文档中说 CLang 中的模块支持是部分的。我在最近发布的 LLVM 12.0 的 Windows 64 位下使用 CLang。
我成功地使用了常规模块(通过import modulename; 导入)。
但我还没有设法创建和使用标题单元模块,即您通过import "header.hpp"; 导入的那些。您能否通过示例建议如何做到这一点?
为了尝试标题单元,我创建了下一个玩具文件:
hello.hpp:
#include <vector>
使用.cpp:
import "hello.hpp";
int main() {
std::vector<int> v(123);
}
然后我成功(我希望)将头单元hello.hpp编译成PCM文件:
clang++ -std=c++20 -Xclang -emit-header-module -I. hello.hpp -o hello.pcm
命令运行没有错误并产生hello.pcm。如果您在没有-o 标志的情况下运行上面的命令,则文件
hello.hpp.gch 已创建。
然后我尝试编译use.cpp,但没有成功,不知何故它无法识别我的标头单元和/或找不到对应的hello.pcm。我想我缺少一些特殊标志,这些标志显示编译器它是头单元。使用了下一个命令:
clang++ -std=c++20 -fprebuilt-module-path=. -fmodule-file=hello.hpp=hello.pcm -I. use.cpp
编译错误:
use.cpp:1:8: error: header file "hello.hpp" (aka './hello.hpp') cannot be imported because it is not known to be a header unit
import "hello.hpp";
^
在 MSVC 下,我成功地使用了常规模块和标头单元模块。但不是在 Clang 中。你能帮我解决这个问题吗?或者告诉我可能还不支持 CLang 标头单元。
【问题讨论】:
标签: c++ clang c++20 c++-modules