如何使用Godbolt(Compiler Explorer)测试C++模块?
如何在Compiler Explorer中实现C++20模块的编译测试
当然可以!在Compiler Explorer上完全能实现你说的C++20模块编译测试,具体步骤我给你拆解一下:
添加并配置模块源文件
点击界面左上角的「Add」按钮,新建一个名为test.cpp的文件,填入模块代码:export module test; export template<typename T> void do_something(const T&) { }选中这个文件后,在右侧的编译选项框里替换默认内容为:
-std=c++20 -stdlib=libc++ -fmodules -c -Xclang -emit-module-interface -o test.pcm这一步会生成模块接口文件
test.pcm。添加并配置主程序文件
再新建一个main.cpp文件,填入主程序代码:import test; int main() { do_something(7); }选中
main.cpp后,右侧编译选项设置为:-std=c++20 -stdlib=libc++ -fmodules -fimplicit-modules -fimplicit-module-maps -fprebuilt-module-path=.这里的
-fprebuilt-module-path=.用来指定模块接口文件的查找路径,Compiler Explorer会把编译生成的文件放在当前工作目录根目录下,所以用.就可以。编译运行
先编译test.cpp生成模块接口,再编译main.cpp,就能完成整个模块的导入和运行测试了。
小提示:尽量选择较新版本的Clang(比如Clang 14及以上)来编译,确保对C++20模块的支持更完善。
内容的提问来源于stack exchange,提问作者Rumburak
相关产品推荐
相关产品推荐

