模块编译单元组织方式
面试考点
C++20 模块(Modules)是替代头文件的新编译模型。面试官通过这道题考察你是否理解模块的基本概念、模块接口单元与实现单元的区别、分区(partition)的用法、以及模块相比头文件的优势。
核心概念
模块是一种将代码组织为独立编译单元的方式,通过 export 和 import 控制可见性。模块只编译一次,导入模块不会重复解析代码,也不会受到宏的污染。
模块单元类型
| 单元类型 | 声明方式 | 作用 |
|---|---|---|
| 主接口单元 | export module M; | 每个模块必须有且只有一个,定义模块的公开接口 |
| 实现单元 | module M; | 可以有多个,包含模块的内部实现 |
| 接口分区 | export module M:Part; | 模块接口的子部分,可被主接口导出 |
| 实现分区 | module M:Part; | 模块内部共享的实现代码 |
基本示例
cpp
// ============================
// 示例1:最简单的模块
// ============================
// --- math.cppm(模块接口单元)---
export module math; // 声明这是模块 math 的接口
// export 的声明对导入者可见
export int add(int a, int b) {
return a + b;
}
export int multiply(int a, int b) {
return a * b;
}
// 没有 export 的声明是模块内部的,导入者看不到
int internal_helper(int x) {
return x * x;
}
// --- main.cpp ---
import math; // 导入模块 math
#include <iostream>
int main() {
std::cout << add(3, 4) << std::endl; // 7,可以访问
std::cout << multiply(3, 4) << std::endl; // 12,可以访问
// internal_helper(5); // 编译错误:未导出,不可见
return 0;
}
// ============================
// 示例2:分离接口和实现
// ============================
// --- geometry.cppm(模块接口单元)---
export module geometry;
// 只声明,不定义
export double circle_area(double radius);
export double rectangle_area(double width, double height);
// --- geometry_impl.cpp(模块实现单元)---
module geometry; // 没有 export,这是实现单元
#include <cmath> // 头文件只在实现单元中 include
// 实现接口中声明的函数
double circle_area(double radius) {
return M_PI * radius * radius;
}
double rectangle_area(double width, double height) {
return width * height;
}
// ============================
// 示例3:模块分区
// ============================
// --- shapes-circle.cppm(接口分区)---
export module shapes:circle; // shapes 模块的 circle 分区
export struct Circle {
double radius;
double area() const;
};
// --- shapes-rect.cppm(接口分区)---
export module shapes:rect; // shapes 模块的 rect 分区
export struct Rectangle {
double width, height;
double area() const;
};
// --- shapes.cppm(主接口单元)---
export module shapes;
// 导出分区,外部用户只需要 import shapes
export import :circle;
export import :rect;
// --- shapes_impl.cpp(实现单元)---
module shapes;
#include <cmath>
double Circle::area() const {
return M_PI * radius * radius;
}
double Rectangle::area() const {
return width * height;
}
// --- main.cpp ---
// import shapes; // 自动获得 Circle 和 Rectangle
// ============================
// 示例4:全局模块片段(兼容旧代码)
// ============================
// --- network.cppm ---
module; // 全局模块片段开始
#include <sys/socket.h> // 旧的 C 头文件放在这里
#include <netinet/in.h> // 避免宏污染模块
export module network; // 全局模块片段结束,模块声明开始
export class TcpSocket {
public:
bool connect(const char* host, int port);
void close();
private:
int fd_ = -1;
};
// ============================
// 示例5:私有模块片段
// ============================
// --- config.cppm ---
export module config;
export class Config {
public:
int get(const char* key) const;
};
module : private; // 私有片段,以下内容不影响导入者
// 修改私有片段的内容不会导致导入者重新编译
int Config::get(const char* key) const {
// 实现细节
return 42;
}
// ============================
// 示例6:C++23 标准库模块
// ============================
/*
C++23 提供了两个标准库模块:
import std; // 导入整个标准库(不含 C 兼容头文件)
import std.compat; // 导入标准库 + C 兼容名称(如全局 printf)
用法:
import std;
int main() {
std::cout << std::format("Hello, {}!", "world") << std::endl;
std::vector<int> v = {1, 2, 3};
return 0;
}
*/模块 vs 头文件
| 特性 | 模块 | 头文件 |
|---|---|---|
| 编译次数 | 只编译一次 | 每个翻译单元都重新解析 |
| 宏隔离 | 宏不泄漏 | 宏会污染后续代码 |
| 包含顺序 | 无影响 | 顺序可能导致不同结果 |
| 编译速度 | 快(预编译模块接口) | 慢(重复解析) |
| 封装性 | 只有 export 的才可见 | 头文件中所有内容都可见 |
| include guard | 不需要 | 需要 #pragma once 或 guard |
常见问题
模块文件用什么扩展名?
标准没有规定。常见约定:MSVC 用 .ixx,GCC/Clang 用 .cppm 或 .mpp。普通实现单元仍然可以用 .cpp。
模块能导出宏吗?
不能。模块不导出预处理器宏。如果需要宏,把它放在传统头文件中用 #include 引入。这是模块的设计决策,避免宏的全局污染。
模块分区和子模块有什么区别?
分区(module M:Part)是一个模块的内部组成部分,外部只能导入整个模块 M,不能单独导入分区。子模块(module M.Sub)是独立的模块,名字用点分隔只是约定,没有特殊的语义关系,可以独立导入。
适用场景
- 大型项目的代码组织
- 替代头文件减少编译时间
- 库的接口封装
- 避免宏污染和包含顺序问题
注意事项
- 模块编译有依赖顺序(接口单元必须先编译)
- 编译器对模块的支持程度不同,需要确认工具链版本
- 全局模块片段用于兼容旧的 C/C++ 头文件
- 私有模块片段的修改不会触发导入者重新编译
- 模块中的 inline 函数需要在接口单元中定义
总结
C++20 模块通过 export/import 替代 #include,实现了真正的编译隔离。模块只编译一次、不泄漏宏、不受包含顺序影响。模块单元分为主接口单元、实现单元、接口分区和实现分区。面试中要能解释模块和头文件的区别、模块分区的组织方式、全局模块片段的作用、以及 C++23 标准库模块(import std)。