Skip to content

Commit 01220d0

Browse files
authored
Merge pull request #3522 from v-albemi/header-unit-walkthroughs
edit pass: Header unit walkthroughs
2 parents 3b11c22 + ac2ca93 commit 01220d0

File tree

2 files changed

+130
-129
lines changed

2 files changed

+130
-129
lines changed

docs/build/walkthrough-header-units.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
description: "Learn more about C++ header units by converting a header file to a header unit"
3-
title: "Walkthrough: Build and import header units in Microsoft Visual C++ projects"
2+
description: "Learn more about C++ header units by converting a header file to a header unit by using Visual Studio 2019."
3+
title: "Walkthrough: Build and import header units in Visual C++ projects"
44
ms.date: "4/13/2021"
55
ms.custom: "conceptual"
66
author: "tylermsft"
@@ -10,46 +10,46 @@ helpviewer_keywords: ["import", "header unit", "ifc"]
1010

1111
# Walkthrough: Build and import header units in Microsoft Visual C++
1212

13-
This article is about building and importing header units using Visual Studio 2019. See [Walkthrough: Import STL libraries using header units](walkthrough-import-stl-header-units.md) to learn specifically how to import Standard Template Library headers as header units.
13+
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.
1414

15-
Header units are the recommended alternative to [precompiled header files](creating-precompiled-header-files.md) (PCH). They're easier to set up and easier to use than a [shared PCH](https://devblogs.microsoft.com/cppblog/shared-pch-usage-sample-in-visual-studio), while providing 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 [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.
1616

1717
## Prerequisites
1818

19-
Support for header units requires at least Visual Studio 2019 16.10.0 Preview 2.
19+
To use header units, you need Visual Studio 2019 16.10.0 Preview 2 or later.
2020

21-
## What is a header unit
21+
## What is a header unit?
2222

23-
Header units are a binary representation of a header file, and end with an *`.ifc`* extension. This is also the format used for named modules.
23+
A header unit is a binary representation of a header file. A header unit ends with an *`.ifc`* extension. This format is also used for named modules.
2424

25-
An important difference between a header unit and a header file is that header units aren't affected by macro definitions. You can't `#define` a symbol that causes the header unit to behave differently when you import it, the way that you can with a header file.
25+
An important difference between a header unit and a header file is that header units aren't affected by macro definitions. You can't `#define` a symbol that causes the header unit to behave differently when you import it. You can do that with a header file.
2626

2727
A similarity is that everything visible from a header file is also visible from a header unit.
2828

29-
Before you can import a header unit, a header file must be compiled into a header unit. An advantage of header units over a PCH is that it 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 machine 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 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.
3030

31-
Another advantage of header units over PCHs is that there's more flexibility when it comes to 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. But with header units, the primary flags that should be the same include:
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:
3232

33-
- Exception handling switches such as `/EHsc`
34-
- `/MD[d]` or `MT[d]`
35-
- `/D` You can define additional macros when building the program that imports the header unit, but those used to build the header unit should also be present and defined the same way when building the program that imports the header unit.
33+
- Exception handling switches like `/EHsc`.
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.
3636

3737
## Ways to compile a header unit
3838

3939
There are several ways to compile a file into a header unit:
4040

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. The reason it's better suited to smaller projects is because it can't guarantee optimal build throughput since it scans all of the files to find what should be built into header units.
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.
4242

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 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.
4444

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 quickly and selectively try out header units in your project. This approach is demonstrated in this walkthrough.
4646

4747
## Convert a project to use header units
4848

49-
In this example, you'll compile a header file as a header unit. Begin by creating the following project in Visual Studio:
49+
In this example, you'll compile a header file as a header unit. Start by creating the following project in Visual Studio:
5050

5151
1. Create a new C++ console app project.
52-
1. Replace the source file contents as follows:
52+
1. Replace the source file content as follows:
5353
```cpp
5454
#include "Pythagorean.h"
5555

@@ -59,7 +59,7 @@ In this example, you'll compile a header file as a header unit. Begin by creatin
5959
return 0;
6060
}
6161
```
62-
1. Add a header file called `Pythagorean.h`, and replace its contents with the following:
62+
1. Add a header file called `Pythagorean.h`, and replace its content with this code:
6363
```cpp
6464
#pragma once
6565
#include <iostream>
@@ -70,36 +70,36 @@ In this example, you'll compile a header file as a header unit. Begin by creatin
7070
}
7171
```
7272

73-
To enable header units, first set the **C++ Language Standard** to [`/std:c++latest`](./reference/std-specify-language-standard-version.md):
73+
To enable header units, first set the **C++ Language Standard** to [`/std:c++latest`](./reference/std-specify-language-standard-version.md).
7474

75-
1. From the Visual Studio main menu, choose **Project** > **Properties**.
76-
1. In the left-hand pane of the project property pages window, select **Configuration Properties** > **General**.
77-
1. Change the **C++ Language Standard** dropdown to **Preview-Features from the Latest C++ Working Draft (/std:c++latest)**.
78-
:::image type="content" source="media/set-cpp-language-latest.png" alt-text="Screenshot showing setting the language standard to preview version.":::
75+
1. On the Visual Studio main menu, select **Project** > **Properties**.
76+
1. In the left pane of the project property pages window, select **Configuration Properties** > **General**.
77+
1. In the **C++ Language Standard** list, select **Preview - Features from the Latest C++ Working Draft (/std:c++latest)**:
78+
:::image type="content" source="media/set-cpp-language-latest.png" alt-text="Screenshot that shows setting the language standard to the preview version.":::
7979

8080
### Compile a header file as a header unit
8181

82-
In the **Solution Explorer**, select the file you want to compile as a header unit. Right-click that file, and select **Properties**. Then do one of the following, depending on the file type:
82+
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:
8383

8484
**For header files**:
8585

86-
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)** for you.
87-
:::image type="content" source="media/change-item-type.png" alt-text="Screenshot showing changing the item type to c/c++ compiler.":::
86+
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+
:::image type="content" source="media/change-item-type.png" alt-text="Screenshot that shows changing the item type to C/C++ compiler.":::
8888

89-
**For source files** (or header files that don't have a `.h` or `.hpp` extension):
89+
**For source files** (or header files that don't have an *`.h`* or *`.hpp`* extension):
9090

9191
Set the **Compile As** property to **Compile as C++ Header Unit (/exportHeader)**.
92-
:::image type="content" source="media/change-compile-as.png" alt-text="Screenshot showing changing Compile As to Compile as C++ Header Unit.":::
92+
:::image type="content" source="media/change-compile-as.png" alt-text="Screenshot that shows changing Compile As to Compile as C++ Header Unit.":::
9393

9494
### Change your code to import a header unit
9595

96-
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 is 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";`
96+
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";`.
9797

98-
Build the solution (**Build** > **Build Solution** from the main menu) and run it to see that it produces the expected output: `Pythagorean triple a:2 b:3 c:13`
98+
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`
9999

100100
In your own projects, repeat this process to compile the header files you want to import as header units.
101101

102-
If you only want to convert a few header files to header units, this is a good approach. But if you have many header files that you want to compile, and the convenience of having the build system automatically handle it outweighs the potential impact on build performance, you can have the build system scan for, and build, header units for you. See [Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach1) to learn how.
102+
If you want to convert only a few header files to header units, this approach is a good one. But if you have many header files that you want to compile, and the convenience of having the build system automatically handle it outweighs the potential effect on build performance, you can have the build system scan for and build header units for you. See [Walkthrough: Import STL libraries as header units](walkthrough-import-stl-header-units.md#approach1) to learn how.
103103

104104
## See also
105105

0 commit comments

Comments
 (0)