Skip to content

Commit 779bfe0

Browse files
TylerMSFTTylerMSFTShannonLeavitt
authored
Twhitney updateui (#4082)
* update ui steps * simplify * fix formatting * try a note * formatting * bridge the note * edits * edits * acrolinx * Update docs/build/walkthrough-header-units.md Co-authored-by: TylerMSFT <Tyler.Whitney@microsoft.com> Co-authored-by: Shannon Leavitt <V-SHLEAV@microsoft.com>
1 parent a1b8adf commit 779bfe0

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

docs/build/walkthrough-header-units.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about C++ header units. Convert a header file to a header unit using Visual Studio 2022."
33
title: "Walkthrough: Build and import header units in Visual C++ projects"
4-
ms.date: 02/03/2022
4+
ms.date: 02/04/2022
55
ms.custom: "conceptual"
66
author: "tylermsft"
77
ms.author: "twhitney"
@@ -52,7 +52,7 @@ There are several ways to compile a file into a header unit:
5252

5353
## <a name="approach1"></a>Approach 1: Choose header units to build
5454

55-
This section show how to choose a specific file to translate into a header unit. Compile a header file as a header unit using the following steps in Visual Studio:
55+
This section shows how to choose a specific file to translate into a header unit. Compile a header file as a header unit using the following steps in Visual Studio:
5656

5757
1. Create a new C++ console app project.
5858
1. Replace the source file content as follows:
@@ -84,30 +84,30 @@ This section show how to choose a specific file to translate into a header unit.
8484

8585
### Set project properties
8686

87-
To enable header units, first set the **C++ Language Standard** to [`/std:c++20`](./reference/std-specify-language-standard-version.md) or later:
88-
1. On the Visual Studio main menu, select **Project** > **Properties**.
87+
To enable header units, first set the **C++ Language Standard** to [`/std:c++20`](./reference/std-specify-language-standard-version.md) or later by using the following steps:
88+
89+
1. In **Solution Explorer**, right-click the project name and choose **Properties**.
8990
1. In the left pane of the project property pages window, select **Configuration Properties** > **General**.
90-
1. In the **C++ Language Standard** list, select **ISO C++20 Standard (/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)**.
91+
1. In the **C++ Language Standard** dropdown, select **ISO C++20 Standard (/std:c++20)** or later. Choose **Ok** to close the dialog.
9192

9293
Compile the header file as a header unit:
9394

94-
1. 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**.
95-
1. 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**. This property also sets **C/C++** > **Advanced** > **Compile As** to **Compile as C++ Header Unit (/exportHeader)**.
95+
1. In **Solution Explorer**, select the file you want to compile as a header unit (in this case, `Pythagorean.h`). Right-click the file and choose **Properties**.
96+
1. Set the **Configuration properties** > **General** > **Item Type** dropdown to **C/C++ compiler** and choose **Ok**.
9697

9798
:::image type="content" source="media/change-item-type.png" alt-text="Screenshot that shows changing the item type to C/C++ compiler.":::
9899

99-
The following isn't necessary for this walkthrough, but to compile a file that doesn't have a `.h` or `.hpp` extension as a header unit, you would set the **Compile As** property to **Compile as C++ Header Unit (/exportHeader)**:
100+
Later in this walkthrough, when you build this project, `Pythagorean.h` will be translated into a header unit. That's because the item type for this header file is set to **C/C++ compiler**, and because the default action for `.h` and `.hpp` files set this way is to translate the file into a header unit.
100101

101-
:::image type="content" source="media/change-compile-as.png" alt-text="Screenshot that shows changing Configuration properties > C/C++ > Advanced > Compile As to Compile as C++ Header Unit (/exportHeader).":::
102+
> [!NOTE]
103+
> This isn't required for this walkthrough, but is provided for your information. To compile a file as a header unit that doesn't have a default header unit file extension, like `.cpp` for example, set **Configuration properties** > **C/C++** > **Advanced** > **Compile As** to **Compile as C++ Header Unit (/exportHeader)**:
104+
> :::image type="content" source="media/change-compile-as.png" alt-text="Screenshot that shows changing Configuration properties > C/C++ > Advanced > Compile As to Compile as C++ Header Unit (/exportHeader).":::
102105
103106
### Change your code to import a header unit
104107

105-
In the source file for the example project, change `#include "Pythagorean.h"` to `import "Pythagorean.h";` Don't forget the trailing semicolon that's required for `import` statements.
106-
107-
To compile a header unit from a system header, use angle brackets: `import <file>;`\
108-
If it's a header in a directory local to your project, use quotes: `import "file";`
108+
1. In the source file for the example project, change `#include "Pythagorean.h"` to `import "Pythagorean.h";` Don't forget the trailing semicolon that's required for `import` statements. Because it's a header file in a directory local to the project, we used quotes with the `import` statement, that is: `import "file";` In your own projects, to compile a header unit from a system header, use angle brackets: `import <file>;`
109109

110-
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`
110+
1. 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`
111111

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

@@ -117,20 +117,20 @@ If you're interested in specifically importing STL library headers as header uni
117117

118118
## <a name="approach2"></a>Approach 2: Automatically scan for and build header units
119119

120-
Due to scanning all of your source files to find and build header units, this approach is best suited for smaller projects because it doesn't guarantee optimal build throughput .
120+
Due to scanning all of your source files to find and build header units, this approach is best suited for smaller projects because it doesn't guarantee optimal build throughput.
121121

122122
This approach combines two Visual Studio project settings:
123123

124124
- **Scan Sources for Module Dependencies** causes the build system to call the compiler to ensure that all imported modules and header units are built before compiling the files that depend on them. When combined with **Translate Includes to Imports**, header files included in your source, that are also specified in a [`header-units.json`](./reference/header-unit-json-reference.md) file in the same directory as the header file, are compiled into header units.
125125
- **Translate Includes to Imports** treats a header file as an `import` if the `#include` refers to a header file that can be compiled as a header unit (as specified in a `header-units.json` file), and a compiled header unit is available for the header file. Otherwise, the header file is treated as a normal `#include`. The [`header-units.json`](./reference/header-unit-json-reference.md) file is used to automatically build header units for each `#include` without symbol duplication.
126126

127-
You can turn these settings on in the properties for your project. To do so, right-click the project in the **Solution Explorer** and choose **Properties**. Then choose **Configuration Properties** > **C/C++** > **General**.
127+
You can turn on these settings in the properties for your project. To do so, right-click the project in the **Solution Explorer** and choose **Properties**. Then choose **Configuration Properties** > **C/C++** > **General**.
128128

129129
:::image type="content" source="media/vs2019-scan-module-dependencies.png" alt-text="Screenshot that shows the project properties screen with Configuration highlighted and All Configurations selected. Under C/C++ > General, Scan Sources for Module Dependencies is highlighted and set to yes, and Translate Includes to Imports is highlighted and set to Yes (/translateInclude)":::
130130

131131
**Scan Sources for Module Dependencies** can be set for all of the files in the project in **Project Properties** as shown here, or for individual files in **File Properties**. Modules and header units are always scanned. Set this option when you have a `.cpp` file that imports header units that you want built automatically and might not be built yet.
132132

133-
These settings work to together to automatically build and import header units under these conditions:
133+
These settings work together to automatically build and import header units under these conditions:
134134

135135
- **Scan Sources for Module Dependencies** scans your sources for files, and their dependencies, that can be treated as header units. Files that have the extension `.ixx`, and those which have their **File properties** > **C/C++** > **Compile As** property set to **Compile as C++ Header Unit (/export)**, are always scanned regardless of this setting. The compiler also looks for `import` statements to identify header unit dependencies. If `/translateInclude` is specified, the compiler also scans for `#include` directives that are also specified in a `header-units.json` file to treat as header units. A dependency graph is built of all the modules and header units in your project.
136136
- **Translate Includes to Imports** When the compiler encounters an `#include` statement, and a matching header unit file (`.ifc`) exists for the specified header file, the compiler imports the header unit instead of treating the header file as an `#include`. When combined with **Scan for dependencies**, the compiler finds all of the header files that can be compiled into header units. An allowlist is consulted by the compiler to decide which header files can compile into header units. This list is stored in a [`header-units.json`](./reference/header-unit-json-reference.md) file that must be in the same directory as the included file. You can see an example of a `header-units.json` file under the installation directory for Visual Studio. For example, `%ProgramFiles%\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.30.30705\include\header-units.json` is used by the compiler to determine whether a Standard Template Library header can be compiled into a header unit. This functionality exists to serve as a bridge with legacy code to get some benefits of header units.

0 commit comments

Comments
 (0)