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 103d01a
Browse filesBrowse the repository at this point in the historyBrowse files
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.
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).
14
14
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
16
16
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:
18
18
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.
20
41
21
42
## Schema
22
43
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
+
23
46
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`
24
47
25
48
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.
Copy file name to clipboardExpand all lines: docs/build/reference/translateinclude.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
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "/translateInclude (Translate include directives into import directives)"
3
3
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
5
5
author: "tylermsft"
6
6
ms.author: "twhitney"
7
7
f1_keywords: ["/translateInclude"]
@@ -11,7 +11,7 @@ helpviewer_keywords: ["/translateInclude", "Translate include directives into im
11
11
12
12
This switch has two functions:
13
13
* 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).
15
15
16
16
## Syntax
17
17
@@ -22,7 +22,7 @@ This switch has two functions:
22
22
`/translateInclude` is available starting in Visual Studio 2019 version 16.10.\
23
23
The **`/translateInclude`** compiler option requires [/std:c++20](std-specify-language-standard-version.md) or a later option such as **`/std:c++latest`**.
24
24
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:
26
26
27
27
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.
28
28
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).
Copy file name to clipboardExpand all lines: docs/build/walkthrough-compile-a-c-program-on-the-command-line.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ If you've installed Microsoft Visual C++ Build Tools 2015 on Windows 10 or later
61
61
62
62
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.
63
63
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:
Copy file name to clipboardExpand all lines: docs/build/walkthrough-header-units.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,19 +116,21 @@ This approach is best suited to smaller projects because it can't guarantee opti
116
116
117
117
This approach combines two Visual Studio project settings:
118
118
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.
120
120
-**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`.
121
121
122
122
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**:
123
123
124
124
:::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)":::
125
125
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
+
126
128
There are conditions under which these settings work to automatically build and import header units. They are:
127
129
128
130
-**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.
129
131
- **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.
130
132
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:
0 commit comments