Skip to content

Commit 103d01a

Browse files
TylerMSFTTylerMSFT
authored andcommitted
modules-cpp.md
2 parents 15c0f36 + c8d132f commit 103d01a

13 files changed

Lines changed: 126 additions & 98 deletions

‎docs/build/reference/experimental-module.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: "/experimental:module (Enable module support)"
33
description: "Use the /experimental:module compiler option to enable experimental compiler support for named modules."
4-
ms.date: "04/13/2021"
4+
ms.date: 01/27/2022
55
f1_keywords: ["module", "/experimental:module"]
66
helpviewer_keywords: ["module", "/experimental:module", "Enable module support"]
77
---
88
# `/experimental:module` (Enable module support)
99

10-
Enables experimental compiler support for C++ Standard modules. This option is obsolete in Visual Studio version 16.11 and later.
10+
Enables experimental compiler support for C++ Standard modules. This option is obsolete for C++20 standard modules in Visual Studio version 16.11 and later. It's still required (along with [`/std:c++latest`](std-specify-language-standard-version.md)) for the experimental Standard library modules.
1111

1212
## Syntax
1313

‎docs/build/reference/header-unit-json-reference.md‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Reference for header-units.json file"
33
title: "C++ header unit.json reference"
4-
ms.date: 01/11/2022
4+
ms.date: 01/31/2022
55
author: "tylermsft"
66
ms.author: "twhitney"
77
f1_keywords: ["header-units.json"]
@@ -10,16 +10,39 @@ helpviewer_keywords: ["header-units.json", "header unit"]
1010

1111
# C++ header-units.json reference
1212

13-
The `header-units.json` file lists which header files can be automatically built by the build system into header units, and then treated as an `import` instead of an `#include`.
13+
The `header-units.json` file lists which header files can be built by the build system into header units. This file must be in the same directory as the header files being included. This file is only used when [/translateInclude](translateinclude.md) is specified along with either [/scanDependencies](scandependencies.md) or [/sourceDependencies:directives]([`/sourceDependencies`](sourcedependencies.md).
1414

15-
Sometimes a header file can't be compiled into a header unit. For example, `<cassert>` shouldn't be compiled as a header unit because it depends on a `#define` at compile time to determine its behavior. Using `#define` can't be used to change the behavior of a header unit, so `<cassert>` shouldn't be compiled into a header unit because it wouldn't provide the expected behavior.
15+
## Rationale
1616

17-
When the command-line switch [`/translateInclude` (Translate include directives into import directives)](translateinclude.md) is used, the build system looks for `#include` files that can be compiled as header units.
17+
Some header files can't be compiled into header units. For example, given `a.h`, `b.h` and `macros.h` which are all in the same directory:
1818

19-
There's an allowlist for the Standard Template Library (STL) headers that the build system consults when `/translateInclude` is used. The build system uses it to determine whether to create a header unit for an STL header file, and for its dependencies. If the STL header file isn't on the list, it's treated as a normal `#include` instead of importing it as a header unit.
19+
**a.h**
20+
```cpp
21+
#include "macros.h" // #defines MACRO=1
22+
#ifdef MACRO
23+
#include "b.h"
24+
#endif
25+
```
26+
27+
The `header-units.json` in this directory can contain `a.h` and `b.h`, but not `macros.h`. It would look something like this:
28+
29+
```json
30+
{
31+
"Version": "1.0",
32+
"BuildAsHeaderUnits": [
33+
// macros.h should not be listed
34+
"a.h",
35+
"b.h"
36+
]
37+
}
38+
```
39+
40+
The reason `macros.h` can't be listed in the `headerunits.json` file is that during the scan phase, the header unit (`.ifc`) may not be compiled yet for `macros.h`, so `MACRO` may not be defined whn `a.h` is compiled. That results in `b.h` missing from the list of dependencies for `a.h`, so the build system won't build a header unit for `b.h` despite it being listed in the `header-units.json` file. To avoid this problem, which results from a dependency on a macro in another header file, the header file defining the macro is excluded from the list of header files that can be compiled into a header unit.
2041

2142
## Schema
2243

44+
There's a `headerunits.json` file for the Standard Template Library (STL) headers. The build system uses it to determine whether to create a header unit for an STL header file, and for its dependencies. If the STL header file isn't on the list, it's treated as a normal `#include` instead of importing it as a header unit.
45+
2346
You can see the `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`
2447

2548
The `header-units.json` file starts with the schema version, followed by an array of filenames for headers that can be built into header units.

‎docs/build/reference/makefile-preprocessing.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Expressions can use the following operators. The operators of equal precedence a
9696
| **`~`** | Unary one's complement. |
9797
| **`-`** | Unary negation. |
9898
| | |
99-
| **`*`*** | Multiplication. |
99+
| **`*`** | Multiplication. |
100100
| **`/`** | Division. |
101101
| **`%`** | Modulus (remainder). |
102102
| | |
@@ -120,7 +120,7 @@ Expressions can use the following operators. The operators of equal precedence a
120120
| | |
121121
| **`&&`** | Logical AND. |
122122
| | |
123-
| **` | | `** | Logical OR. |
123+
| **` || `** | Logical OR. |
124124

125125
> [!NOTE]
126126
> The bitwise XOR operator (**`^`**) is the same as the escape character, and must be escaped (as **`^^`**) when it's used in an expression.

‎docs/build/reference/translateinclude.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "/translateInclude (Translate include directives into import directives)"
33
description: "Use the /translateInclude compiler option to treat #include directives as import statements when an importable header unit is available."
4-
ms.date: 01/28/2022
4+
ms.date: 01/31/2022
55
author: "tylermsft"
66
ms.author: "twhitney"
77
f1_keywords: ["/translateInclude"]
@@ -11,7 +11,7 @@ helpviewer_keywords: ["/translateInclude", "Translate include directives into im
1111

1212
This switch has two functions:
1313
* Instructs the compiler to treat `#include` as `import` for header files that have been built into header unit (`.ifc`) files and that are specified on the command line with [`/headerUnit` (Use header unit IFC)](headerunit.md).
14-
* When used in conjunction with [`/scanDependencies`](scandependencies.md) or [`/sourceDependencies-directives`](sourcedependencies-directives.md) and a [`header-units.json`](header-unit-json-reference.md) file (which specifies which header files can be compiled as header units), the compiler generates a JSON file that lists module and header-unit dependencies according to C++ Standard proposal [`P1689R3`](https://wg21.link/P1689r3). This dependency file is used by the build system to generate compiled header unit files (`.ifc` files). Then, instead of running the specified header files through the preprocessor, they are imported. For more details, see [Walkthrough: Build and import header units in Microsoft Visual C++](../walkthrough-header-units.md).
14+
* When used in conjunction with [`/scanDependencies`](scandependencies.md) or [`/sourceDependencies-directives`](sourcedependencies-directives.md), the compiler lists headers that are included both in the source and also listed in a [`header-units.json`](header-unit-json-reference.md) file as imported header units in the generated dependency file. This dependency file is used by the build system to generate compiled header unit files (`.ifc` files). Then, instead of running the specified header files through the preprocessor, they are imported. For more details, see [Walkthrough: Build and import header units in Microsoft Visual C++](../walkthrough-header-units.md).
1515

1616
## Syntax
1717

@@ -22,7 +22,7 @@ This switch has two functions:
2222
`/translateInclude` is available starting in Visual Studio 2019 version 16.10.\
2323
The **`/translateInclude`** compiler option requires [/std:c++20](std-specify-language-standard-version.md) or a later option such as **`/std:c++latest`**.
2424

25-
This following outlines what the Microsoft build system does when **`/translateInclude`** and `/scanDependencies` or `/sourceDependencies:directives` is specified:
25+
This following outlines what the Microsoft build system does when **`/translateInclude`** and [`/scanDependencies`]() or [`/sourceDependencies:directives`](sourcedependencies-directives.md) is specified:
2626

2727
1. Get all header units and modules from referenced projects and add them, as well as their dependencies, via `/reference` and `/headerUnit` to the command line for all sources in the project.
2828
1. The compiler scans all of the sources that are marked to be scanned. They are marked by default based on file extension, or may be marked explicitly in the IDE. For an example of marking files explicitly, see [Walkthrough: Build and import header units in Microsoft Visual C++](../walkthrough-header-units.md).

‎docs/build/walkthrough-compile-a-c-program-on-the-command-line.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ If you've installed Microsoft Visual C++ Build Tools 2015 on Windows 10 or later
6161

6262
If you're using a different version of Windows, look in your Start menu or Start page for a Visual Studio tools folder that contains a developer command prompt shortcut. You can also use the Windows search function to search for "developer command prompt" and choose one that matches your installed version of Visual Studio. Use the shortcut to open the command prompt window.
6363

64-
Next, verify that the Visual C++ developer command prompt is set up correctly. In the command prompt window, enter `cl` and verify that the output looks something like this:
64+
Next, verify that the Visual C++ developer command prompt is set up correctly. In the command prompt window, enter `cl` (or `CL`, case doesn't matter for the compiler name, but it does matter for compiler options). The output should look something like this:
6565

6666
```Output
6767
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise>cl

‎docs/build/walkthrough-header-units.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,21 @@ This approach is best suited to smaller projects because it can't guarantee opti
116116

117117
This approach combines two Visual Studio project settings:
118118

119-
- **Scan Sources for Module Dependencies** scans your project for files and their dependencies that may be treated as header units. When combined with **Translate Includes to Imports**, header files that are specified in a `header-units.json` file are compiled into header units.
119+
- **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 file that depends on them. When combined with **Translate Includes to Imports**, header files that are specified in a `header-units.json` file are also compiled into header units.
120120
- **Translate Includes to Imports** If an `#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, then the header file is treated treated as `import`. Otherwise, it acts as a normal `#include`.
121121

122122
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**:
123123

124124
:::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)":::
125125

126+
**Scan Sources for Module Dependencies option** can be set for all of the files in the project (in **Project Properties** as shown above) or for individual files in **File Properties**. Modules and header units are always scanned. Set this option is when you have a `.cpp` file that imports header units that aren't built yet and that you want to build automatically.
127+
126128
There are conditions under which these settings work to automatically build and import header units. They are:
127129

128130
- **Scan Sources for Module Dependencies** scans your sources for files, and their dependencies, that may 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 could be treated as header units. A dependency graph is built of all the modules and header units in your project.
129131
- **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 running the header file through the preprocessor. When combined with **Scan for dependencies**, the compiler finds all of the header files that can be compiled into header units. An allow-list 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 when determining 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.
130132

131-
The `header-units.json` file serves two roles. In addition to specifying which header files can be compiled into header units, it helps minimize duplicated symbols and thus increase build throughput. For instance, given:
133+
The `header-units.json` file serves two roles. In addition to specifying which header files can be compiled into header units, it minimizes duplicated symbols to increase build throughput. For instance, given:
132134

133135
```cpp
134136
a.h:

0 commit comments

Comments
 (0)