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
This article is about building and importing header units by using Visual Studio 2019. See [Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md) to learn specifically how to import Standard Template Library headers as header units.
14
14
15
-
Header units are the recommended alternative to [precompiled header files](creating-precompiled-header-files.md) (PCH). They're easier to set up and use than [shared PCH](https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio), but they provide similar performance benefits. Unlike a PCH, when a header unit changes, only it and its dependencies are rebuilt.
15
+
Header units are the recommended alternative to [precompiled header files](creating-precompiled-header-files.md) (PCH). They're easier to set up and use than a [shared PCH](https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio), but they provide similar performance benefits. Unlike a PCH, when a header unit changes, only it and what depends on it are rebuilt.
16
16
17
17
## Prerequisites
18
18
@@ -26,27 +26,27 @@ An important difference between a header unit and a header file is that header u
26
26
27
27
A similarity is that everything visible from a header file is also visible from a header unit.
28
28
29
-
Before you can import a header unit, you need to compile a header file into a header unit. An advantage of header units over PCH is that they can be used in distributed builds. For example, as long as you're using the same compiler to compile the *`.ifc`* and the program that imports it and are targeting the same platform and architecture, a header unit produced on one computer can be used on another.
29
+
Before you can import a header unit, you need to compile a header file into a header unit. An advantage of header units over PCH is that they can be used in distributed builds. For example, as long as youcompile the *`.ifc`* and the program that imports it with the same compiler, and target the same platform and architecture, a header unit produced on one computer can be used on another.
30
30
31
-
Another advantage of header units over PCH is that there's more flexibility for the compiler flags used to compile the header unit and for the program that imports it. With a PCH, more compiler flags must be the same. But with header units, these flags should be the same:
31
+
Another advantage of header units over PCH is that there's more flexibility for differences between the compiler flags used to compile the header unit and the program that imports it. With a PCH, more compiler flags must be the same. With header units, only the following flags need to be the same:
32
32
33
33
- Exception handling options like **`/EHsc`**.
34
34
-**`/MD[d]`** or **`MT[d]`**.
35
-
-**`/D`**. You can define additional macros when you build the program that imports the header unit. But the ones used to build the header unit should also be present and defined the same way when you build the program that imports the header unit.
35
+
-**`/D`**. You can define other macros when you build the program that imports the header unit. But the ones used to build the header unit should also be present and defined the same way when you build the program that imports the header unit.
36
36
37
37
## Ways to compile a header unit
38
38
39
39
There are several ways to compile a file into a header unit:
40
40
41
41
-**Automatically scan for header units**. This approach is best suited to smaller projects that include many different header files. See [Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach1) for a demonstration of this approach. This approach is better suited to smaller projects because it can't guarantee optimal build throughput. That's because it scans all the files to find what should be built into header units.
42
42
43
-
-**Build a shared header unit project**. This approach is best suited for larger projects and for when you want more control over the organization of the imported header units. You create a static library project (or projects) that contain the header units that you want. Then reference the library project (or projects) from the project that then imports the header units it needs. See [Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach2) for a demonstration of this approach.
43
+
-**Build a shared header unit project**. This approach is best suited for larger projects and for when you want more control over the organization of the imported header units. You create a static library project (or projects) that contain the header units that you want. Then reference that library project (or projects) to import the header units. See [Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach2) for a demonstration of this approach.
44
44
45
-
-**Choose individual header units to build**. This approach gives you file-by-file control over which header files are treated as header units. It's also a good way to quickly and selectively try out header units in your project. This approach is demonstrated in this walkthrough.
45
+
-**Choose individual header units to build**. This approach gives you file-by-file control over which header files are treated as header units. It's also a good way to try out header units in your project. This approach is demonstrated in this walkthrough.
46
46
47
47
## Convert a project to use header units
48
48
49
-
In this example, you'll compile a header file as a header unit. Start by creating the following project in Visual Studio:
49
+
Compile a header file as a header unit using the following steps in Visual Studio:
50
50
51
51
1. Create a new C++ console app project.
52
52
1. Replace the source file content as follows:
@@ -61,7 +61,7 @@ In this example, you'll compile a header file as a header unit. Start by creatin
61
61
}
62
62
```
63
63
64
-
1. Add a header file called `Pythagorean.h`, and replace its content with this code:
64
+
1. Add a header file called `Pythagorean.h` and then replace its content with this code:
65
65
66
66
```cpp
67
67
#ifndef PYTHAGOREAN
@@ -76,31 +76,29 @@ In this example, you'll compile a header file as a header unit. Start by creatin
76
76
#endif
77
77
```
78
78
79
-
To enable header units, first set the **C++ Language Standard** to [`/std:c++20`](./reference/std-specify-language-standard-version.md) or later.
79
+
To enable header units, first set the **C++ Language Standard** to [`/std:c++20`](./reference/std-specify-language-standard-version.md) or later:
80
80
81
81
1. On the Visual Studio main menu, select **Project** > **Properties**.
82
82
1. In the left pane of the project property pages window, select **Configuration Properties** > **General**.
83
-
1. In the **C++ Language Standard** list, select **ISO C++20Standard (/std:c++20)** or later. (In versions before Visual Studio 2019 version 16.11, select **Preview - Features from the Latest C++ Working Draft (/std:c++latest)**.)
83
+
1. In the **C++ Language Standard** list, select **ISO C++20Standard (/std:c++20)** or later. In versions before Visual Studio 2019 version 16.11, select **Preview - Features from the Latest C++ Working Draft (/std:c++latest)**.
84
84
85
85
### Compile a header file as a header unit
86
86
87
-
In **Solution Explorer**, select the file you want to compile as a header unit. Right-click the file and select **Properties**. Then do one of the following, depending on the file type:
88
-
89
-
**For header files**:
90
-
91
-
Set the **Item Type** property to **C/C++ compiler**. By default, header files have an **Item Type** of **C/C++ header**. Setting this property also sets **C/C++** > **Advanced** > **Compile As** to **Compile as C++ Header Unit (/exportHeader)**.
87
+
In **Solution Explorer**, select the file you want to compile as a header unit (in this case, `Pythagorean.h`). Right-click the file and select **Properties**. Since this is a header file, set the **Item Type** property to **C/C++ compiler**. By default, header files have an **Item Type** of **C/C++ header**. Setting this property also sets **C/C++** > **Advanced** > **Compile As** to **Compile as C++ Header Unit (/exportHeader)**.
92
88
:::image type="content" source="media/change-item-type.png" alt-text="Screenshot that shows changing the item type to C/C++ compiler.":::
93
89
94
-
**For source files** (or header files that don't have an *`.h`* or *`.hpp`* extension):
90
+
**Compile a source file as a header unit**:
95
91
96
-
Set the **Compile As** property to **Compile as C++ Header Unit (/exportHeader)**.
92
+
If you want to compile a file that doesn't have an *`.h`* or *`.hpp`* extension as a header unit, set the **Compile As** property to **Compile as C++ Header Unit (/exportHeader)**.
97
93
:::image type="content" source="media/change-compile-as.png" alt-text="Screenshot that shows changing Compile As to Compile as C++ Header Unit.":::
98
94
99
95
### Change your code to import a header unit
100
96
101
-
In the source file for the example project, that is, the file that contains `main()`, change `#include "Pythagorean.h"` to `import "Pythagorean.h";`. (Don't forget the trailing semicolon that's required for `import` statements.) When you're compiling a header unit from a system header, use angle brackets (`import <file>;`). If it's a project header, use `import "file";`.
97
+
In the source file for the example project that contains `main()`, change `#include "Pythagorean.h"` to `import "Pythagorean.h";`. Don't forget the trailing semicolon that's required for `import` statements.
98
+
99
+
When compiling a header unit from a system header, use angle brackets (`import <file>;`). If it's a project header, use `import "file";`.
102
100
103
-
Build the solution. (**Build** > **Build Solution** on the main menu.) Run it to see that it produces the expected output: `Pythagorean triple a:2 b:3 c:13`
101
+
Build the solution by selecting **Build** > **Build Solution** on the main menu. Run it to see that it produces the expected output: `Pythagorean triple a:2 b:3 c:13`
104
102
105
103
In your own projects, repeat this process to compile the header files you want to import as header units.
0 commit comments