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
Resolve syncing conflicts from FromPrivateLiveToMaster to main (#4433)
* rewording ReadString documentation to be more accurate (#4815)
* reword nmax parameter wording for ReadString to be more accurate
* change suggested directory location
* acrolinx
* incorp review
* fix path
* improve path
* Confirm merge from FromPublicMasterBranch to main to sync with https://github.com/MicrosoftDocs/cpp-docs (branch main) (#4817)
* Update vcperf-commands.md (#4375)
* Update vcperf-commands.md
Update commands according to https://github.com/microsoft/vcperf#command-line-reference
* Update vcperf-commands.md
* Update with grammar fixes for i18n
---------
Co-authored-by: Colin Robertson <[email protected]>
* Update tutorial-import-stl-named-module.md (#4429)
* Update tutorial-import-stl-named-module.md
The Title of the document doesn't reveal that this procedure is only for a command line case. Suggest altering the titles to reflect this and improve clarity for readers searching for module documentation.
* Update tutorial-import-stl-named-module.md
Changed the capitalization at Microsoft's request.
---------
Co-authored-by: huypub <[email protected]>
Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com>
Co-authored-by: Nelson Daniel Troncoso <[email protected]>
Co-authored-by: Colin Robertson <[email protected]>
Co-authored-by: Centurion Maximus <[email protected]>
* fix casing and wording
* acrolinx
---------
Co-authored-by: Anju del Moral Gonzalez <[email protected]>
Co-authored-by: TylerMSFT <[email protected]>
Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com>
Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com>
Co-authored-by: huypub <[email protected]>
Co-authored-by: Nelson Daniel Troncoso <[email protected]>
Co-authored-by: Colin Robertson <[email protected]>
Co-authored-by: Centurion Maximus <[email protected]>
Co-authored-by: Jak Koke <[email protected]>
Copy file name to clipboardExpand all lines: docs/cpp/tutorial-import-stl-named-module.md
+18-17Lines changed: 18 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
---
2
2
title: "Tutorial: Import the standard library (STL) using modules from the command line (C++)"
3
-
ms.date: 12/02/2022
3
+
ms.date: 2/23/2023
4
4
ms.topic: "tutorial"
5
5
author: "tylermsft"
6
6
ms.author: "twhitney"
7
7
helpviewer_keywords: ["modules [C++]", "modules [C++]", "named modules [C++], import standard library (STL) using named modules"]
8
-
description: Learn how to import the C++ standard library (STL) using modules
8
+
description: Learn how to import the C++ standard library (STL) using modules from the command line
9
9
---
10
-
# Tutorial: Import the C++ standard library using modules from the Command Line
10
+
11
+
# Tutorial: Import the C++ standard library using modules from the command line
11
12
12
13
Learn how to import the C++ standard library using C++ library modules. This is significantly faster to compile and more robust than using header files or header units or precompiled headers (PCH).
13
14
@@ -32,35 +33,35 @@ It's now possible to import the standard library as a module instead of as a tan
32
33
33
34
The C++23 standard library introduces two named modules: `std` and `std.compat`.
34
35
35
-
-`std` exports the declarations and names defined in the C++ standard library that are in namespace `std` such as `std::vector` and `std::sort`. It also exports the contents of C wrapper headers such as `<cmath>`, `<cstdio>`, `<cstdlib>` that provide `std::byte`, `std::printf()`, and so on. The C functions defined in the *global namespace* such as `::printf()` and `::fopen`aren't exported. This improves the situation where previously including a C wrapper header like `<cstdio>`which provides `std::` qualified versions of the C runtime functions *also* included the actual C header, like `stdio.h`, which meant the C global namespace versions were also brought in. This is no longer a problem if you import `std`.
36
-
-`std.compat` exports everything in `std`, and adds the global namespace counterparts of the C runtime such as `::printf`, `::fopen`, `::size_t`, `::strlen`, and so on. This module makes it easier when working with a codebase that refers to many C runtime functions/types in the global namespace.
36
+
-`std` exports the declarations and names defined in the C++ standard library namespace `std` such as `std::vector` and `std::sort`. It also exports the contents of C wrapper headers such as `<cstdio>` and `<cstdlib>`, which provide functions like `std::printf()`. C functions defined in the *global namespace*, such as `::printf()`, aren't exported. This improves the situation where including a C wrapper header like `<cstdio>`*also* included C header files like `stdio.h`, which brought in the C global namespace versions. This is no longer a problem if you import `std`.
37
+
-`std.compat` exports everything in `std` and adds the global namespace counterparts of the C runtime such as `::printf`, `::fopen`, `::size_t`, `::strlen`, and so on. This module makes it easier to work with a codebase that refers to many C runtime functions/types in the global namespace.
37
38
38
39
The compiler imports the entire standard library when you use `import std;` or `import std.compat;` and does it faster than bringing in a single header file. That is, it's faster to bring in the entire standard library with `import std;` (or `import std.compat`) than it's to `#include <vector>`, for example.
39
40
40
41
Because named modules don't expose macros, macros like `assert`, `errno`, `offsetof`, `va_arg`, and others aren't available when you import `std` or `std.compat`. See [Standard library named module considerations](#standard-library-named-module-considerations) for workarounds.
41
42
42
43
## A little about C++ modules
43
44
44
-
Header files are how declarations and definitions have been shared between source files in C++. Prior to standard library modules, you'd include the part of the standard library you needed with a directive like `#include <vector>`. Header files are fragile and difficult to compose because their semantics may change depending on the order you include them, or whether certain macros are or aren't defined. They also slow compilation because they have to be reprocessed by every source file that includes them.
45
+
Header files are how declarations and definitions have been shared between source files in C++. Prior to standard library modules, you'd include the part of the standard library you needed with a directive like `#include <vector>`. Header files are fragile and difficult to compose because their semantics may change depending on the order you include them, or whether certain macros are or aren't defined. They also slow compilation because they're reprocessed by every source file that includes them.
45
46
46
47
C++20 introduces a modern alternative called *modules*. In C++23, we were able to capitalize on module support to introduce named modules for the standard library.
47
48
48
-
Like header files, modules allow you to share declarations and definitions across source files. But unlike header files, modules aren't fragile and are easier to compose because their semantics don't change due to macro definitions or the order in which you import them. The compiler can process modules significantly faster than it can process `#include` files, and uses less memory at compile time as well. Macro definitions defined in a named module aren't exposed, nor are private implementation details.
49
+
Like header files, modules allow you to share declarations and definitions across source files. But unlike header files, modules aren't fragile and are easier to compose because their semantics don't change due to macro definitions or the order in which you import them. The compiler can process modules significantly faster than it can process `#include` files, and uses less memory at compile time as well. Named modules don't expose macro definitions or private implementation details.
49
50
50
51
For in depth information about modules, see [Overview of modules in C++](modules-cpp.md) That article also discusses consuming the C++ standard library as modules, but uses an older and experimental way of doing it.
51
52
52
53
This article demonstrates the new and best way to consume the standard library. For more information about alternative ways to consume the standard library, see [Compare header units, modules, and precompiled headers](../build/compare-inclusion-methods.md).
53
54
54
55
## Import the standard library with `std`
55
56
56
-
The following examples demonstrates how to consume the standard library as a module using the command line compiler. The Visual Studio IDE experience for importing the standard library as a module is still being implemented.
57
+
The following examples demonstrate how to consume the standard library as a module using the command line compiler. The Visual Studio IDE experience for importing the standard library as a module is being implemented.
57
58
58
-
The standard library is imported into your application with the statement `import std;` or `import std.compat;`. But first, you must compile the standard library named modules. The following steps demonstrate how.
59
+
The statement `import std;` or `import std.compat;` imports the standard library into your application. But first, you must compile the standard library named modules. The following steps demonstrate how.
59
60
60
61
### Example: `std`
61
62
62
63
1. Open a x86 Native Tools Command Prompt for VS: from the Windows **Start** menu, type *x86 native* and the prompt should appear in the list of apps. Ensure that the prompt is for Visual Studio 2022 preview version 17.5 or above. You'll get errors if you use the wrong version of the prompt. The examples used in this tutorial are for the CMD shell. If you use PowerShell, substitute `"$Env:VCToolsInstallDir\modules\std.ixx"` for `"%VCToolsInstallDir\modules\std.ixx"`.
63
-
1. Create a directory such as `C:\STLModules`, and make it the current directory.
64
+
1. Create a directory, such as `%USERPROFILE%\source\repos\STLModules`, and make it the current directory. If you choose a directory that you don't have write access to, you'll get errors such as not being able to open the output file `std.ifc`.
64
65
1. Compile the `std` named module with the following command:
65
66
66
67
```dos
@@ -71,9 +72,9 @@ The standard library is imported into your application with the statement `impor
71
72
72
73
You should compile the `std` named module using the same compiler settings that you intend to use with the code that imports it. If you have a multi-project solution, you can compile the standard library named module once, and then refer to it from all of your projects by using the [`/reference`](../build/reference/module-reference.md) compiler option.
73
74
74
-
Using the compiler command above, the compiler outputs two files:
75
+
Using the previous compiler command, the compiler outputs two files:
75
76
- `std.ifc` is the compiled binary representation of the named module interface that the compiler consults to process the `import std;` statement. This is a compile-time only artifact. It doesn't ship with your application.
76
-
- `std.obj` contains the implementation of the named module. Add `std.obj` to the command line when you compile the sample app so that functionality you use from the standard library can be statically linked into your application.
77
+
- `std.obj` contains the implementation of the named module. Add `std.obj` to the command line when you compile the sample app to statically link the functionality you use from the standard library into your application.
77
78
78
79
The key command-line switches in this example are:
79
80
@@ -131,14 +132,14 @@ The standard library is imported into your application with the statement `impor
131
132
132
133
The C++ standard library includes the ISO C standard library. The `std.compat` module provides all of the functionality of the `std` module like `std::vector`, `std::cout`, `std::printf`, `std::scanf`, and so on. But it also provides the global namespace versions of these functions such as `::printf`, `::scanf`, `::fopoen`, `::size_t`, and so on.
133
134
134
-
The `std.compat` named module is a compatibility layer provided to ease migrating existing code that refers to C runtime functions in the global namespace. If you want to avoid adding names to the global namespace, use `import std;`. If you need to ease migrating a codebase that uses many unqualified (that is, global namespace) C runtime functions, use `import std.compat;`. This provides the global namespace C runtime names so that you don't have to qualify all the global name mentions with `std::`. If you don't have any existing code that uses the global namespace C runtime functions, then you don't need to use `import std.compat;`. If you only call a few C runtime functions in your code, it may be better to use `import std;` and qualify the few global namespace C runtime names that need it with `std::`, for example, `std::printf()`. You'll know that you should consider using `import std.compat;` if you see an error like `error C3861: 'printf': identifier not found` when you try to compile your code.
135
+
The `std.compat` named module is a compatibility layer provided to ease migrating existing code that refers to C runtime functions in the global namespace. If you want to avoid adding names to the global namespace, use `import std;`. If you need to ease migrating a codebase that uses many unqualified (that is, global namespace) C runtime functions, use `import std.compat;`. This provides the global namespace C runtime names so that you don't have to qualify all the global name mentions with `std::`. If you don't have any existing code that uses the global namespace C runtime functions, then you don't need to use `import std.compat;`. If you only call a few C runtime functions in your code, it may be better to use `import std;` and qualify the few global namespace C runtime names that need it with `std::`, for example, `std::printf()`. If you see an error like `error C3861: 'printf': identifier not found` when you try to compile your code, consider using `import std.compat;` to import the global namespace C runtime functions.
135
136
136
137
### Example: `std.compat`
137
138
138
139
Before you can use `import std.compat;` in your code, you must compile the named module `std.compat.ixx`. The steps are similar to for building the `std` named module. The steps include building the `std` named module first because `std.compat` depends on it:
139
140
140
-
1. Open a Native Tools Command Prompt for VS: from the Windows **Start** menu, type *x86 native* and the prompt should appear in the list of apps. Ensure that the prompt is for Visual Studio 2022 preview version 17.5 or above. If you use the wrong version of the prompt, you'll get compiler errors.
141
-
1. Create a directory to try this example, such as `C:\STLCompatModules`, and make it the current directory.
141
+
1. Open a Native Tools Command Prompt for VS: from the Windows **Start** menu, type *x86 native* and the prompt should appear in the list of apps. Ensure that the prompt is for Visual Studio 2022 preview version 17.5 or above. You'll get compiler errors if you use the wrong version of the prompt.
142
+
1. Create a directory to try this example, such as `%USERPROFILE%\source\repos\STLModules`, and make it the current directory. If you choose a directory that you don't have write access to, you'll get errors such as not being able to open the output file `std.ifc`.
142
143
1. Compile the `std` and `std.compat` named modules with the following command:
143
144
144
145
```dos
@@ -153,7 +154,7 @@ Before you can use `import std.compat;` in your code, you must compile the named
153
154
- `std.ifc` is the compiled binary named module interface that the compiler consults to process the `import std;` statement. The compiler also consults `std.ifc` to process `import std.compat;` because `std.compat` builds on `std`. This is a compile-time only artifact. It doesn't ship with your application.
154
155
- `std.obj` contains the implementation of the standard library.
155
156
- `std.compat.ifc` is the compiled binary named module interface that the compiler consults to process the `import std.compat;` statement. This is a compile-time only artifact. It doesn't ship with your application.
156
-
- `std.compat.obj` contains implementation. However, most of the implementation is provided by `std.obj`. Add `std.obj` to the command line as well when you compile the sample app so that the functionality you use from the standard library can be statically linked into your application.
157
+
- `std.compat.obj` contains implementation. However, most of the implementation is provided by `std.obj`. Add `std.obj` to the command line when you compile the sample app to statically link the functionality that you use from the standard library into your application.
157
158
158
159
1. To try out importing the `std.compat` library, create a file named `stdCompatExample.cpp` with the following content:
159
160
@@ -192,7 +193,7 @@ Before you can use `import std.compat;` in your code, you must compile the named
192
193
193
194
I've shown them as separate steps in this tutorial because you only need to build the standard library named modules once, and then you can refer to them from your project, or from multiple projects. But it may be convenient to build them all at once.
194
195
195
-
The compiler command above produces an executable named `stdCompatExample.exe`. When you run it, it produces the following output:
196
+
The previous compiler command produces an executable named `stdCompatExample.exe`. When you run it, it produces the following output:
196
197
197
198
```output
198
199
Import std.compat to get global names like printf()
0 commit comments