跳到主要内容

modules

C++20 Modules 概览

最小示例:

接口单元 math.ixx

export module math;

export int add(int a, int b) { return a + b; }

使用方:

import math;
int main(){ return add(1,2); }

构建方式随编译器而异(需支持模块):

  • Clang/GCC: 配合 CMake 的 CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API 或工具链文档。