# 支持 C++20 模块

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/zh-cn/help/clion/llms.txt).

[模块](https://en.cppreference.com/w/cpp/language/modules)是在 [C++20](https://en.cppreference.com/w/cpp/20) 标准中引入的。 它们作为头文件某些用例的替代方案，并有助于在翻译单元之间共享声明和定义。

> **Tip:**
> 目前，CLion 不将  `.cpp`  文件视为模块，因此建议您使用其他扩展名（例如，  `.cppm` ）。

CLion 从  `.ixx` 、  `.cppm`  和  `.mxx`  文件中收集信息，解析  `export module {name}`  语句，并在  `{name}`  和模块文件名之间创建映射。 然后，这些映射会传递给 [Clangd 引擎](settings-languages-cpp-clangd.html)，并用于提供代码辅助和高亮功能。

如果您更新了模块代码或重命名了模块，CLion 将自动解析更改。 从模块中收集的信息会在 IDE 重启之间保留。

CLion 对模块的支持已在以下环境中进行了测试：

* CMake、Ninja 生成器、Visual Studio C++ 工具链。

* CMake、Visual Studio 生成器、Visual Studio C++ 工具链。

* CMake、带有编译器标志的 Clang 工具链。

> **Tip:**
> 有关项目配置的更多信息，请参阅 [CMake 生成器](cmake-profile.html#cmake-generators)、 [MSVC](quick-tutorial-on-configuring-clion-on-windows.html#MSVC)、 [编译器](how-to-switch-compilers-in-clion.html) 和 [快速 CMake 教程](quick-cmake-tutorial.html)。

## 模块的代码洞察

* CLion 高亮显示模块关键字（ `import` 、 `export` 和 `module` ）： ![模块关键字高亮](https://resources.jetbrains.com/help/img/idea/2026.3/cl_modules_highlight.png)

* 补全适用于模块中的符号： ![模块符号的补全](https://resources.jetbrains.com/help/img/idea/2026.3/cl_modules_completion.png)

* 您可以使用 `Ctrl+B` (Windows), `⌘ B` (macOS), `⌘ B` (IntelliJ IDEA Classic (macOS)), `⌘ B` (macOS System Shortcuts), `Ctrl+B` (XWin), `Ctrl+B` (GNOME), `Ctrl+B` (KDE), `Ctrl+Alt+G` (Emacs), `F12` (Sublime Text), `F12` (Sublime Text (macOS)), `⌘ Click` (Xcode), `F12` (Visual Studio), `F12` (Visual Studio (macOS)), `Ctrl+B` (ReSharper), `⌘ B` (ReSharper (macOS)), `F2` (QtCreator), `F2` (QtCreator (macOS)), `Ctrl+B` (NetBeans), `F3` (Eclipse), `F3` (Eclipse (macOS)) 在模块中符号的声明和定义之间导航： ![模块的导航](https://resources.jetbrains.com/help/img/idea/2026.3/cl_modules_navigation.animated.gif)

* [查找用法](find-highlight-usages.html) 和 [重构](refactoring-source-code.html) 可在模块内部使用。 还有早期版本的 [重命名](rename-refactorings.html)、 [更改签名](change-signature.html) 和 [提取](extract-method.html) 重构，这些功能可以跨模块边界工作（目前，它们仅适用于在 CLion 中打开的文件）。

## 添加模块

在 CLion 中，您可以自动将新模块添加到项目中。

Procedure:

1. 在项目树中，右键点击要添加模块的文件夹，然后选择  `新建 | C++ 模块接口单元` ：

![新建模块菜单](https://resources.jetbrains.com/help/img/idea/2026.3/cl_newmodules_menu.png)

2. 指定模块名称、其类型（扩展名）以及是否希望 CLion 将新创建的模块添加到现有的 CMake 目标：

![新建模块对话框](https://resources.jetbrains.com/help/img/idea/2026.3/cl_newmodule_dialog.png)

3. 如果您选择了该选项，将在项目树中创建一个新模块，并将其添加到  `CMakeLists.txt`  脚本中：

![新建模块已创建](https://resources.jetbrains.com/help/img/idea/2026.3/cl_newmodule_results.png)

您可以在  `设置 | 编辑器 | 文件和代码模板`  中编辑 CLion 用于创建模块文件的模板：

![C++ 模块文件模板设置](https://resources.jetbrains.com/help/img/idea/2026.3/cl_newmodule_codestule_Settings.png)

## See also

### 外部链接

[isocpp.org: articles tagged with 'Modules'](https://isocpp.org/blog/tag/modules) [CLion blog: announcement of modules support](https://blog.jetbrains.com/clion/2022/10/clion-2022-3-eap-cpp20-modules-now-supported/) [Modules description at ccpreference.com](https://en.cppreference.com/w/cpp/language/modules)

### 流程

[Clangd settings](settings-languages-cpp-clangd.html) [C++ keywords completion](c-keywords-completion.html)

