std20使用模块(visual studio模块文件格式.ixx)
模块可以做库
// my_math.ixx
export module my_math;
export template <typename T>
T sum2(T a,T b)
{
return a + b;
}
// main.cpp
import my_math;// 在my_math.ixx同级目录
#include <iostream>
using namespace std;
int main()
{
cout << sum2<int>(1,2) << endl;// 会有红线,但是编译通过
}
g++单模块编译(g++12)
// my_math.cpp
// 源码同上 my_math.ixx
// main.cpp
// 源码同上
编译
g++ -fmodules-ts my_math.cpp main.cpp -o main
查看是否成功