You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 764c70b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/build/compare-inclusion-methods.md
+17-5Lines changed: 17 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,25 +7,37 @@ helpviewer_keywords: ["headers, C++ library", "libraries, Standard C++", "C++ St
7
7
---
8
8
# Compare header units, modules, and precompiled headers
9
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.
10
+
Historically, you'd include the standard library with a directive like `#include <vector>`. It's expensive to include header files because they're processed by every source file that includes them.
11
+
12
+
Precompiled headers (PCH) were introduced to speed compilation by translating them once and reusing the result.
13
+
14
+
In C++20, modules were introduced as a significant improvement on header files and precompiled headers.
15
+
16
+
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.
17
+
18
+
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
19
12
20
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
21
14
22
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
23
16
24
| Method | Summary |
17
25
|---|---|
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. |
26
+
|`#include`| This is the 'classic' way of doing things. On disadvantage is that they exposes macros and internal implementation. Internal implementation is often exposed as functions and types that start with an underscore, which is a convention to indicate that it's internal and shouldn't be used. Header files are fragile because the order of #includes can modify behavior or break code and are affected by macro definitions. Header files slow compilation, particularly when the same file is included in multiple files because the header file has to be processed for each file that includes it. |
19
27
|[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
28
|[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. |
29
+
| [Modules](../cpp/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. |
30
+
31
+
If you can use C++20 features and the C++23 standard library, use named modules.
32
+
33
+
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.
22
34
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.
35
+
If you can't use C++20 features, header units, or precompiled headers, you're left with `#include`, which is the slowest and least robust method.
Copy file name to clipboardExpand all lines: docs/cpp/tutorial-import-stl-named-module.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,9 +47,9 @@ C++20 introduces a modern alternative called *modules*. In C++23, we were able t
47
47
48
48
Like header files, modules allow you to share declarations and definitions across source files. But unlike header files, modules aren't fragile and are easier to compose because their semantics don't change due to macro definitions or the order in which they're imported. The compiler can process modules significantly faster than it can process `#include` files, and uses less memory at compile time as well. Macro definitions defined in a named module aren't exposed, nor are private implementation details.
49
49
50
-
For more information about the different ways to include standard library functionality in C++, see [Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md).
50
+
For in depth information about modules, see [Overview of modules in C++](../build/modules-cpp.md) That article also discusses consuming the C++ standard library as modules, but uses an older and experimental way of doing it.
51
51
52
-
For in depth information about modules, see [Overview of modules in C++](modules-cpp.md) That article also discusses consuming the C++ standard library as modules, but uses an older and experimental way of doing it. This article demonstrates the new and best way to consume the standard library. For more information about alternative ways to consume the standard library, see [Different ways to consume the standard library](#different-ways-to-consume-the-standard-library).
52
+
This article demonstrates the new and best way to consume the standard library. For more information about alternative ways to consume the standard library, see [Different ways to consume the standard library](#different-ways-to-consume-the-standard-library).
53
53
54
54
## Import the standard library with `std`
55
55
@@ -221,7 +221,7 @@ In this tutorial, you've imported the standard library using modules. Next, lear
221
221
222
222
## See also
223
223
224
-
[Compare header units, modules, and precompiled headers](compare-header-units-modules-and-precompiled-headers.md)\
224
+
[Compare header units, modules, and precompiled headers](../build/compare-inclusion-methods.md)\
225
225
[Overview of modules in C++](modules-cpp.md)\
226
226
[A Tour of C++ Modules in Visual Studio](https://devblogs.microsoft.com/cppblog/a-tour-of-cpp-modules-in-visual-studio)\
227
227
[Moving a project to C++ named Modules](https://devblogs.microsoft.com/cppblog/moving-a-project-to-cpp-named-modules)
0 commit comments