C++20允许将模块的声明和实现分别定义在不同的cpp文件中:
1.模块的声明
//dec_test.cpp
export module ex_test;
export void test1();
export{
void test2();
};
模块在声明时需要使用export
2.模块的实现
//imo_test.cpp
module;
#include <iostream>
module ex_test;
using namespace std;
void test1()
{
cout<<"this is test1"<<endl;
}
void test2()
{
cout<<"this is test2"<<endl;
}
模块在实现时不需要使用export
3.导入模块:
import ex_test;
int main()
{
test1();
test2();
return 0;
}
编译程序:
g++ -o m dec_test.cpp imp_test.cpp im_main.cpp -std=c++20 -fmodules-ts
允许程序输出:
this is test1
this is test2