Visual Studio features for C++ Header Files and Modules

Mryam Girmay

In the field of C++ programming, the management of header files and modules can often seem challenging. However, there are numerous tools and features available that can simplify this process, enhancing efficiency and reducing the likelihood of errors. Here’s a walkthrough for several tools that we provide for C++ headers and modules.

Run Build Insights

Start by running Build Insights, through navigating to Build > Run Build Insights.

Image Build Insights Image

This tool provides insights into the Include File and Include Tree views to analyze and optimize your #include usage. For instance, in the bullet3 sample repository, running Build Insights reveals that the overall build time of our current project is 202.812 seconds.

Image Build Insights Result

Enable #include Cleanup

Next, enable the #include cleanup feature (Tools > Options > Text Editor > C/C++ > Code Cleanup > Enable #include cleanup). This feature helps remove unused #include directives and add transitively used headers. You can also adjust the severity of the suggestion levels to suit your coding style and requirements.

Image Include cleanup setting

Configure #include Cleanup

You can also customize how #include cleanup generates suggestions using EditorConfig. You can exclude specific includes, specify dependencies, remap headers, and specify alternatives using the setting in Configure Include Cleanup with .editorconfig.

Use #include Cleanup

Utilize the lightbulb in the editor for the following tasks:

Adding Transitively Used Headers: Add indirectly used includes individually or all at once.

Removing Unused Headers: Remove unused includes individually or all at once.

Optimizing Include Graph for this file: Add all transitively used includes and remove unused ones for an optimized include structure.

Let’s apply this to the ‘test_invdyn_jacobian.cpp’ file in our sample repo. Choose “Optimize include graph for test_invdyn_jacobian.cpp”

GIF #include cleanup

Check Build Time Improvement

After cleaning your ‘test_invdyn_jacobian.cpp’ file, run Build Insights again to quantify the benefits of your cleanup efforts. In our sample repo, the overall build time improved to 197.76 seconds.

The #include cleanup feature doesn’t clean your entire project all at once. But you can use it to gradually improve your project. Focus on the parts that take the longest to build. You can find these parts using Build Insights. The more files you clean, the faster your project will build.

Image Build Insights Improved Result

Enable #include Diagnostics

The #include Diagnostics feature allows you to view the usage and build time of your header files directly in your editor.

Turn On #include Diagnostics

Turn on #include diagnostics by right-clicking on the editor to open the context menu, navigating to the ‘#include Directives’ option, and choosing “Turn #include Diagnostics On”. This will display CodeLens above each of your header files, providing information about their usage and build time.

Upon evaluating the build time and usage of each header file, you can employ the #include cleanup feature to refine your headers. This action can markedly improve the overall build time of your project. The reason behind this is that any unused include directive within these headers can cause an unnecessary expansion of the graph wherever the header is utilized. By removing these, you are effectively making the build process more efficient.

GIF #include diagnostics

Use Modules in C++20

In addition to the features mentioned above, C++20 introduces Header Units and Named Modules.

Header Units serve as a bridge between traditional header inclusion and named modules. They allow you to import header files as modules, providing a faster and more robust alternative to the traditional #include. Importantly, they don’t require any changes to your header files but do need significant modifications to your build system.

Named Modules offer superior performance and control over what they export. They require the creation of .ixx files and the addition of export keywords. The order of imports doesn’t matter due to their order-independence and macro-independence.

To dive deeper into these features, check out the Overview of modules in C++ and Walkthrough: Build and import header units in Visual C++ projects on Microsoft Learn.


Send us your feedback

We value your input! If you have any insights, suggestions, or requests, please let us know in the comments section below or in the Developer Community. You can also reach out to us on Twitter (@VisualC) or via email at

Posted in C++


Leave a comment

Feedback usabilla icon