{"id":34065,"date":"2024-04-25T19:25:27","date_gmt":"2024-04-25T19:25:27","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=34065"},"modified":"2024-04-25T20:11:18","modified_gmt":"2024-04-25T20:11:18","slug":"visual-studio-features-for-c-header-files-and-modules","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/visual-studio-features-for-c-header-files-and-modules\/","title":{"rendered":"Visual Studio features for C++ Header Files and Modules"},"content":{"rendered":"<p>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\u2019s a walkthrough for several tools that we provide for C++ headers and modules.<\/p>\n<h3>Run Build Insights<\/h3>\n<p>Start by running <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/build-insights-now-available-in-visual-studio-2022\/\">Build Insights,<\/a> through navigating to <strong>Build <\/strong>&gt;<strong> Run Build Insights<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34071\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1.png\" alt=\"Image Build Insights Image\" width=\"2581\" height=\"737\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1.png 2581w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1-300x86.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1-1024x292.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1-768x219.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1-1536x439.png 1536w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Image-1-2048x585.png 2048w\" sizes=\"(max-width: 2581px) 100vw, 2581px\" \/><\/a><\/p>\n<p>This tool provides insights into the Include File and Include Tree views to analyze and optimize your #include usage. For instance, in the <a href=\"https:\/\/github.com\/bulletphysics\/bullet3\">bullet3<\/a> sample repository, running Build Insights reveals that the overall build time of our current project is 202.812 seconds.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Initial-Result.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34073\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Initial-Result.png\" alt=\"Image Build Insights Result\" width=\"1086\" height=\"483\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Initial-Result.png 1086w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Initial-Result-300x133.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Initial-Result-1024x455.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Initial-Result-768x342.png 768w\" sizes=\"(max-width: 1086px) 100vw, 1086px\" \/><\/a><\/p>\n<h3><\/h3>\n<h3>Enable #include Cleanup<\/h3>\n<p>Next, enable the <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/include-cleanup-overview?view=msvc-170\">#include cleanup<\/a> feature (<strong>Tools <\/strong>&gt; <strong>Options <\/strong>&gt; <strong>Text Editor<\/strong> &gt; <strong>C\/C++ <\/strong>&gt; <strong>Code Cleanup <\/strong>&gt; <strong>Enable #include cleanup<\/strong>). 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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34074\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting.png\" alt=\"Image Include cleanup setting\" width=\"1872\" height=\"1212\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting.png 1872w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting-300x194.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting-1024x663.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting-768x497.png 768w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Include-cleanup-setting-1536x994.png 1536w\" sizes=\"(max-width: 1872px) 100vw, 1872px\" \/><\/a><\/p>\n<h3><\/h3>\n<h3>Configure #include Cleanup<\/h3>\n<p>You can also customize how #include cleanup generates suggestions using <a href=\"https:\/\/learn.microsoft.com\/en-us\/visualstudio\/ide\/create-portable-custom-editor-options?view=vs-2022\">EditorConfig<\/a>. You can exclude specific includes, specify dependencies, remap headers, and specify alternatives using the setting in <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/include-cleanup-config?view=msvc-170#configure-include-cleanup-with-editorconfig\">Configure Include Cleanup with .editorconfig.<\/a><\/p>\n<h3>Use #include Cleanup<\/h3>\n<p>Utilize the lightbulb in the editor for the following tasks:<\/p>\n<p><strong>Adding Transitively Used Headers<\/strong>: Add indirectly used includes individually or all at once.<\/p>\n<p><strong>Removing Unused Headers<\/strong>: Remove unused includes individually or all at once.<\/p>\n<p><strong>Optimizing Include Graph for this file<\/strong>: Add all transitively used includes and remove unused ones for an optimized include structure.<\/p>\n<p>Let\u2019s apply this to the \u2018test_invdyn_jacobian.cpp\u2019 file in our sample repo. Choose \u201cOptimize include graph for test_invdyn_jacobian.cpp\u201d<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/include_cleanup_gif.gif\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34077\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/include_cleanup_gif.gif\" alt=\"GIF #include cleanup\" width=\"938\" height=\"616\" \/><\/a><\/p>\n<h3><\/h3>\n<h3>Check Build Time Improvement<\/h3>\n<p>After cleaning your \u2018test_invdyn_jacobian.cpp\u2019 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.<\/p>\n<p>The #include cleanup feature doesn\u2019t 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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Improved-Result.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34075\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Improved-Result.png\" alt=\"Image Build Insights Improved Result\" width=\"1071\" height=\"503\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Improved-Result.png 1071w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Improved-Result-300x141.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Improved-Result-1024x481.png 1024w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/Build-Insights-Improved-Result-768x361.png 768w\" sizes=\"(max-width: 1071px) 100vw, 1071px\" \/><\/a><\/p>\n<h3><\/h3>\n<h3>Enable #include Diagnostics<\/h3>\n<p>The <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/ide\/include-diagnostics?view=msvc-170\">#include Diagnostics<\/a> feature allows you to view the usage and build time of your header files directly in your editor.<\/p>\n<h3>Turn On #include Diagnostics<\/h3>\n<p>Turn on #include diagnostics by right-clicking on the editor to open the context menu, navigating to the \u2018#include Directives\u2019 option, and choosing \u201cTurn #include Diagnostics On\u201d. This will display CodeLens above each of your header files, providing information about their usage and build time.<\/p>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/include_diagnostics_gif.gif\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-34076\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2024\/04\/include_diagnostics_gif.gif\" alt=\"GIF #include diagnostics \" width=\"1215\" height=\"660\" \/><\/a><\/p>\n<h3><\/h3>\n<h3>Use Modules in C++20<\/h3>\n<p>In addition to the features mentioned above, C++20 introduces Header Units and Named Modules.<\/p>\n<p><strong>Header Units<\/strong> 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\u2019t require any changes to your header files but do need significant modifications to your build system.<\/p>\n<p><strong>Named Modules<\/strong> 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\u2019t matter due to their order-independence and macro-independence.<\/p>\n<p>To dive deeper into these features, check out the <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/cpp\/modules-cpp?view=msvc-170\">Overview of modules in C++<\/a> and <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/build\/walkthrough-header-units?view=msvc-170\">Walkthrough: Build and import header units in Visual C++ projects<\/a> on Microsoft Learn.<\/p>\n<p>&nbsp;<\/p>\n<h3 id=\"send-us-your-feedback\">Send us your feedback<\/h3>\n<p>We value your input! If you have any insights, suggestions, or requests, please let us know in the comments section below <span data-contrast=\"auto\">or\u00a0<\/span><span data-contrast=\"none\">in the\u202f<\/span><a href=\"https:\/\/developercommunity.visualstudio.com\/search?space=62\" target=\"_blank\" rel=\"noopener\"><span data-contrast=\"none\">Developer Community<\/span><\/a>. You can also reach out to us on Twitter (<a href=\"https:\/\/twitter.com\/visualc\">@VisualC<\/a>) or via email at\u00a0<a href=\"mailto:visualcpp@microsoft.com\">visualcpp@microsoft.com<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s a walkthrough for several tools that we provide for C++ headers and modules. Run Build Insights [&hellip;]<\/p>\n","protected":false},"author":96741,"featured_media":34083,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-34065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>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\u2019s a walkthrough for several tools that we provide for C++ headers and modules. Run Build Insights [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/34065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/96741"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=34065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/34065\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/34083"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=34065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=34065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=34065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}