Skip to content

Commit da1529f

Browse files
TylerMSFTTylerMSFT
authored andcommitted
new topic: compare header units, modules, and precompiled headers
1 parent 64e9afa commit da1529f

8 files changed

Lines changed: 67 additions & 32 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
description: "Learn about the different ways to include library headers in C++: header files vs modules vs header units vs precompiled headers."
3+
title: "Compare header units, modules, and precompiled headers"
4+
ms.date: 11/30/2022
5+
f1_keywords: ["#include", "header file", "header unit", "module", "named module", "PCH", "precompiled header unit", "IFC"]
6+
helpviewer_keywords: ["headers, C++ library", "libraries, Standard C++", "C++ Standard Library, headers", "STL", "Standard template library, headers", "precompiled header files, creating", "PCH files, creating", "import", "header unit", "ifc", "modules [C++]", "named modules [C++]", "import standard library (STL) using named modules"]
7+
---
8+
# Compare header units, modules, and precompiled headers
9+
10+
Historically, you'd include the the standard library with a directive like `#include <vector>`. It's expensive to include header files because they are reprocessed over and over by every source file that includes them, so precompiled headers were introduced to speed compilation by translating them once and reusing the result. In C++20, modules were introduced as a significant improvement on header files and precompiled headers. Header units were introduced as a way of temporarily bridging the gap between header files and modules so that you can get some of the speed and robustness benefits of modules while you migrate your code. Then, the C++23 standard library introduced support for importing the standard library as named modules--which is the fastest and most robust way to consume the standard library.
11+
12+
To help you sort out the different options, this article compares the traditional `#include` method against precompiled headers, header units, and the `std` and `std.compat` named modules.
13+
14+
The following table is arranged by compiler processing speed and robustness, with `#include` being the slowest and least robust, and `import` being the fastest and most robust.
15+
16+
| Method | Summary |
17+
|---|---|
18+
| `#include` | This is the 'classic' way of doing things. Disadvantages: exposes macros and internal implementation (functions and types that start with an underscore which library implementors use to indicate it's internal and shouldn't use), is fragile (the order of #includes can modify behavior or break code and are affected by macro definitions), is slow and gets slower when the same file is included in multiple files because the header file has to be reprocessed for each file that includes it. |
19+
| [Precompiled header](../build/creating-precompiled-header-files.md) | A precompiled header (PCH) improves compile time by creating a compiler memory snapshot of a set of header files. This is an improvement on rebuilding header files over and over. PCH files have restrictions that make them difficult to build and maintain. In terms of speed and robustness, PCH files are faster than `#include` but slower than `import`.|
20+
| [Header units](../build/walkthrough-header-units.md) | This is a new feature in C++20 that allows you to import 'well-behaved' header files as modules. Header units are faster than `#include`, and are easier, significantly smaller, and faster than pre-compiled header files (PCH). Header units are an 'in-between' step meant to help transition to named modules in cases where you rely on macros defined in header files, since named modules don't expose macros. Header units are slower than importing a named module. Header units aren't affected by macro defines unless they're specified on the command line when the header unit is built--making them more robust than header files. Header units expose the macros and internal implementation defined in them just as header file do, which named modules don't. As a rough approximation of file size, a 250-megabyte PCH file might be represented by an 80-megabyte header unit file. |
21+
| [Modules](modules-cpp.md) | This is the fastest and most robust way to import functionality. Support for importing modules was introduced in C++20. The C++23 standard library introduces the two named modules described in this topic. When you import `std`, you get the standard names such as `std::vector`, `std::cout`, but no extensions, no internal helpers such as `_Sort_unchecked`, and no macros. The order of imports doesn't matter because there are no macro or other side-effects. As a rough approximation of file size, a 250-megabyte PCH file might be represented by an 80-megabyte header unit file, which might be represented by a 25-megabyte module. The reason named modules are faster is because when a named module is compiled into an `.ifc` file and an `.obj` file, the compiler emits a structured representation of the source code that can be loaded quickly when the module is imported. The compiler can do some work (like name resolution) before emitting the `.ifc` file because of how named modules are order-independent and macro-independent--so this work doesn't have to be done when the module is imported. In contrast, when a header file is consumed with `#include`, its contents must be preprocessed and compiled again and again in every translation unit. Precompiled headers, which are compiler memory snapshots, can mitigate those costs, but not as well as named modules. |
22+
23+
If you can use C++20 features and the C++23 standard library, use named modules. If you can't use C++20 features, use header units. If you can't use C++20 features or header units, use precompiled headers. If you can't use C++20 features, header units, or precompiled headers, you are left with `#include`, which is the slowest and least robust method.
24+
25+
## See also
26+
27+
[Precompiled header files](creating-precompiled-header-files.md)\
28+
[Overview of modules in C++](modules-cpp.md)\
29+
[Tutorial: Import the C++ standard library using modules](../cpp/tutorial-import-stl-named-module.md)\
30+
[Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach1)\
31+
[Walkthrough: Build and import header units in your Visual C++ projects](walkthrough-header-units.md)

docs/build/creating-precompiled-header-files.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ description: "Learn more about: Precompiled header files"
33
title: "Precompiled Header Files"
44
ms.date: 06/29/2022
55
helpviewer_keywords: ["precompiled header files, creating", "PCH files, creating", "cl.exe compiler, precompiling code", ".pch files, creating"]
6-
ms.assetid: e2cdb404-a517-4189-9771-c869c660cb1b
76
---
87
# Precompiled header files
98

@@ -21,7 +20,7 @@ Precompiled code is useful during the development cycle to reduce compilation ti
2120

2221
- You always use a large body of code that changes infrequently.
2322

24-
- Your program comprises multiple modules, all of which use a standard set of include files and the same compilation options. In this case, all include files can be precompiled into one precompiled header.
23+
- Your program comprises multiple modules, all of which use a standard set of include files and the same compilation options. In this case, all include files can be precompiled into one precompiled header. For more information about newer ways to handle include files, see [Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md).
2524

2625
The first compilation (the one that creates the precompiled header file) takes a bit longer than subsequent compilations. Subsequent compilations can proceed more quickly by including the precompiled code.
2726

@@ -341,5 +340,10 @@ int main( void )
341340

342341
## See also
343342

343+
[Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md)\
344344
[C/C++ building reference](reference/c-cpp-building-reference.md)\
345345
[MSVC compiler options](reference/compiler-options.md)
346+
[Overview of modules in C++](modules-cpp.md)\
347+
[Tutorial: Import the C++ standard library using modules](../cpp/tutorial-import-stl-named-module.md)\
348+
[Walkthrough: Build and import header units in your Visual C++ projects](walkthrough-header-units.md)\
349+
[Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach1)

docs/build/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ items:
7878
href: ../build/cmake-predefined-configuration-reference.md
7979
- name: "C++ Build Insights"
8080
href: ../build-insights/get-started-with-cpp-build-insights.md
81-
- name: "Build and import header units"
81+
- name: "Compare header units, modules, and precompiled headers"
82+
href: ../build/compare-inclusion-methods.md
83+
- name: "Header units"
8284
expanded: false
8385
items:
8486
- name: "Walkthrough: Build and import header units in Visual C++ projects"

docs/build/walkthrough-header-units.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This article is about building and importing header units with Visual Studio 202
1515

1616
Header units are the recommended alternative to [precompiled header files](creating-precompiled-header-files.md) (PCH). Header units are easier to set up and use, are significantly smaller on disk, provide similar performance benefits, and are more flexible than a [shared PCH](https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio).
1717

18+
To contrast header units with other ways to include functionality in your programs, see [Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md).
1819
## Prerequisites
1920

2021
To use header units, you need Visual Studio 2019 16.10 or later.
@@ -156,6 +157,7 @@ Enabling the new preprocessor affects the processing of variadic macros. For mor
156157
[`/exportHeader`](./reference/module-exportheader.md)\
157158
[`/headerUnit`](./reference/headerunit.md)\
158159
[`header-units.json`](./reference/header-unit-json-reference.md)\
160+
[Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md)\
159161
[Overview of modules in C++](../cpp/modules-cpp.md)\
160162
[Tutorial: Import the C++ standard library using modules](../cpp/tutorial-import-stl-named-module.md)\
161163
[Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach1)

docs/build/walkthrough-import-stl-header-units.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This walkthrough shows how to import C++ Standard Template Library (STL) librari
1414

1515
Importing an STL header as a header unit is simpler than using [precompiled header files](creating-precompiled-header-files.md). Header units are easier to set up and use, are substantially smaller on disk, provide similar performance benefits, and are more flexible than a [shared PCH](https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio).
1616

17-
For more detailed information about what header units are and the benefits they provide, see [What is a header unit?](walkthrough-header-units.md#what-is-a-header-unit)
17+
For more detailed information about what header units are and the benefits they provide, see [What is a header unit?](walkthrough-header-units.md#what-is-a-header-unit). To contrast header units with other ways to import the standard library, see [Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md).
1818

1919
## Prerequisites
2020

@@ -219,6 +219,7 @@ The main consideration for whether to use this approach is the balance between c
219219
220220
## See also
221221
222+
[Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md)\
222223
[Tutorial: Import the C++ standard library using modules](../cpp/tutorial-import-stl-named-module.md)\
223224
[Walkthrough: Build and import header units in your Visual C++ projects](walkthrough-header-units.md)\
224-
[`/translateInclude`](./reference/translateinclude.md)
225+
[`/translateInclude`](./reference/translateinclude.md)

docs/cpp/modules-cpp.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ C++20 introduces *modules*, a modern solution that turns C++ libraries and progr
1010

1111
You can use modules side by side with header files. A C++ source file can `import` modules and also `#include` header files. In some cases, you can import a header file as a module rather than include it textually by using `#include` in the preprocessor. We recommend you use modules in new projects rather than header files as much as possible. For larger existing projects under active development, experiment with converting legacy headers to modules. Base your adoption on whether you get a meaningful reduction in compilation times.
1212

13+
To contrast modules with other ways to import the standard library, see [Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md).
14+
1315
## Enable modules in the Microsoft C++ compiler
1416

1517
As of Visual Studio 2022 version 17.1, C++20 standard modules are fully implemented in the Microsoft C++ compiler.
@@ -200,4 +202,5 @@ import "myheader.h";
200202
## See also
201203

202204
[`module`, `import`, `export`](import-export-module.md)\
203-
[Named modules tutorial](tutorial-named-modules-cpp.md)
205+
[Named modules tutorial](tutorial-named-modules-cpp.md)\
206+
[Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md)

0 commit comments

Comments
 (0)