{"id":5901,"date":"2020-05-08T10:13:22","date_gmt":"2020-05-08T10:13:22","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/cpp20-modules\/"},"modified":"2023-09-28T06:48:09","modified_gmt":"2023-09-28T06:48:09","slug":"cpp20-modules","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/cpp20-modules\/","title":{"rendered":"C++20: The Advantages of Modules"},"content":{"rendered":"<p>Modules are one of the four big features of C++20: concepts, ranges, coroutines, and modules. Modules promise a lot: compile-time improvement, isolation of macros, the abolition of header files, and ugly workarounds.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8397 size-full\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules.png\" alt=\"\" width=\"1081\" height=\"399\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules.png 1081w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-300x111.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-1030x380.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-768x283.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/09\/TimelineCpp20Modules-705x260.png 705w\" sizes=\"auto, (max-width: 1081px) 100vw, 1081px\" \/><\/p>\n<p>Why do we need modules? I want to step back and describe the steps involved in getting an executable.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"A_Simple_Executable\"><\/span>A Simple Executable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Of course, I have to start with &#8220;Hello World&#8221;.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ helloWorld.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Hello World\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n}\n<\/pre>\n<\/div>\n<p>Making an executable <span style=\"font-family: courier new, courier;\">helloWorld<\/span> out of the program <span style=\"font-family: courier new, courier;\">helloWorld.cpp<\/span> increases its size by a factor 130.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5897\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld.png\" alt=\"helloWorld\" width=\"450\" height=\"180\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld.png 566w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld-300x120.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>The numbers 100 and 12928 in the screenshot stand for the number of bytes.<\/p>\n<p>We should have a basic understanding of what&#8217;s happening under the hood.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_classical_Build_Process\"><\/span>The classical Build Process<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The build process consists of three steps: preprocessing, compilation, and linking.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Preprocessing\"><\/span>Preprocessing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The preprocessor handles the preprocessor directives such as <span style=\"font-family: courier new, courier;\"><code>#include<\/code><\/span> and <span style=\"font-family: courier new, courier;\"><code>#define<\/code><\/span>. The preprocessor substitutes <span style=\"font-family: courier new, courier;\">#inlude<\/span> directives with the corresponding header files, and it substitutes the macros (<span style=\"font-family: courier new, courier;\">#define<\/span>). Thanks to directives such as <code>#if<\/code>, <code>#else<\/code>, <code>#elif<\/code>, <code>#ifdef<\/code>, <code>#ifndef,<\/code> and <code>#endif <\/code>parts of the source code can be included or excluded.<\/p>\n<p>This straightforward text substitution process can be observed using the compiler flag <span style=\"font-family: courier new, courier;\">-E<\/span> on GCC\/Clang, or <span style=\"font-family: courier new, courier;\">\/E<\/span> on Windows.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5898\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/preprocessor.png\" alt=\"preprocessor\" width=\"500\" height=\"149\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/preprocessor.png 547w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/preprocessor-300x89.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>WOW!!! The output of the preprocessing step has more than half a million bytes. I don&#8217;t want to blame GCC; the other compilers are similar verbose: <a href=\"https:\/\/godbolt.org\/z\/rtXGFQ\">CompilerExplorer<\/a>.<\/p>\n<p>The output of the preprocessor is the input for the compiler.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Compilation\"><\/span>Compilation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The compilation is separately performed on each output of the preprocessor. The compiler parses the C++ source code and converts it into assembly code. The generated file is called an object file, containing the compiled code in binary form. The object file can refer to symbols that don&#8217;t have a definition. The object files can be put in archives for later reuse. These archives are called static libraries.<\/p>\n<p>The objects or translation units the compiler produces are the input for the linker.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Linking\"><\/span>Linking<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The linker&#8217;s output can be executable, static, or shared library. The linker&#8217;s job is to resolve the references to undefined symbols. Symbols are defined in object files or libraries. The typical error in this state is that symbols aren&#8217;t defined or defined more than once.<\/p>\n<p>This build process consisting of the three steps is inherited from C. It works sufficiently well enough if you only have one translation unit. But when you have more than one translation unit, many issues can occur.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Issues_of_the_Build_Process\"><\/span>Issues of the Build Process<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Without any attempt to complete it, the classical build process has flaws. Modules overcome these issues.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Repeated_substitution_of_Headers\"><\/span>Repeated substitution of Headers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The preprocessor substitutes <span style=\"font-family: courier new, courier;\">#include<\/span> directives with the corresponding header files. Let me change my initial <span style=\"font-family: courier new, courier;\">helloWorld.cpp<\/span> program to make the repetition visible.<\/p>\n<p>I refactored the program and added two source files <span style=\"font-family: courier new, courier;\">hello.cpp<\/span> and <span style=\"font-family: courier new, courier;\">world.cpp<\/span>. The source file <span style=\"font-family: courier new, courier;\">hello.cpp<\/span> provides the function <span style=\"font-family: courier new, courier;\">hello<\/span> and the source file <span style=\"font-family: courier new, courier;\">world.cpp<\/span> provides the function world. Both source files include the corresponding headers. Refactoring means that the program does the same such as the previous program <span style=\"font-family: courier new, courier;\">helloWorld.cpp.<\/span> The internal structure is changed. Here are the new files:<\/p>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">hello.cpp<\/span> and <span style=\"font-family: courier new, courier;\">hello.h<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ hello.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"hello.h\"<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">hello<\/span>() {\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"hello \"<\/span>;\n}\n<\/pre>\n<\/div>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ hello.h<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">hello<\/span>();<\/pre>\n<\/div>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">world.cpp<\/span> and <span style=\"font-family: courier new, courier;\">world.h<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ world.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"world.h\"<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">world<\/span>() {\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"world\"<\/span>;\n}\n<\/pre>\n<\/div>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ world.h<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">world<\/span>();<\/pre>\n<\/div>\n<ul>\n<li><span style=\"font-family: 'courier new', courier;\">helloWorld2.cpp<\/span><\/li>\n<\/ul>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ helloWorld2.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #009999;\">#include \"hello.h\"<\/span>\n<span style=\"color: #009999;\">#include \"world.h\"<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n    \n    hello(); \n    world(); \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n}\n<\/pre>\n<\/div>\n<p>Building and executing the program works as expected:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5899\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld2.png\" alt=\"helloWorld2\" width=\"600\" height=\"178\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld2.png 790w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld2-300x89.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/helloWorld2-768x227.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>Here is the issue. The preprocessor runs on each source file. This means that the header file <span style=\"font-family: courier new, courier;\">&lt;iostream&gt;<\/span> is included three times in each translation unit. Consequently, each source file is blown up to over half a million lines.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5900\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/preprocessorTranslationUnits.png\" alt=\"preprocessorTranslationUnits\" width=\"500\" height=\"209\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/preprocessorTranslationUnits.png 605w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/preprocessorTranslationUnits-300x125.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>This is a waste of compile time.<\/p>\n<p><strong>In contrast, a module is only imported once and is literally for free.<\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Isolation_from_Preprocessor_Macros\"><\/span>Isolation from Preprocessor Macros<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If there is one consensus in the C++ community, it&#8217;s the following: we should eliminate the preprocessor macros. Why? Using a macro is just text substitution, excluding any C++ semantics. Of course, this has many negative consequences: For example, it may depend on in which sequence you include macros, or macros can clash with already defined macros or names in your application.<\/p>\n<p>Imagine you have two headers<span style=\"font-family: courier new, courier;\"> webcolors.h<\/span> and <span style=\"font-family: courier new, courier;\">productinfo.h.<\/span><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ webcolors.h<\/span>\n\n<span style=\"color: #009999;\">#define RED   0xFF0000<\/span>\n<\/pre>\n<\/div>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ productinfo.h\n<\/span><\/span>\n<span style=\"color: #009999;\">#define RED   0<\/span>\n<\/pre>\n<\/div>\n<p>When a source file\u00a0<span style=\"font-family: courier new, courier;\">client.CPP<\/span> includes both headers, the value of the macro <span style=\"font-family: courier new, courier;\">RED<\/span> depends on the sequence the headers are included. This dependency is very error-prone.<\/p>\n<p><strong>In contrast, it makes no difference in which order you import modules.<\/strong><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Multiple_Definitions_of_Symbols\"><\/span>Multiple Definitions of Symbols<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>ODR stands for the One Definition Rule and says in the case of a function.<\/p>\n<ul>\n<li>A function can have not more than one definition in any translation unit.<\/li>\n<li>A function can have not more than one definition in the program.<\/li>\n<li>Inline functions with external linkage can be defined in more than one translation. The definitions must satisfy the requirement that each must be the same.<\/li>\n<\/ul>\n<p>Let me see what my linker says when I try to link a program breaking the one-definition rule. The following code example has two header files <span style=\"font-family: courier new, courier;\">header.h<\/span> and <span style=\"font-family: courier new, courier;\">header2.h<\/span>. The main program includes the header file header.h twice and, therefore, break the one definition rule because two definitions of <span style=\"font-family: courier new, courier;\">func<\/span> are included.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ header.h<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">func<\/span>() {}\n<\/pre>\n<\/div>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ header2.h<\/span>\n\n<span style=\"color: #009999;\">#include \"header.h\"<\/span>\n<\/pre>\n<\/div>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ main.cpp<\/span>\n\n<span style=\"color: #009999;\">#include \"header.h\"\n<span style=\"color: #009999;\">#include \"header2.h\"<\/span><\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {}\n<\/pre>\n<\/div>\n<p>The linker complains about the multiple definitions of <span style=\"font-family: courier new, courier;\">func<\/span>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5663\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/odr.png\" alt=\"odr\" width=\"500\" height=\"246\" \/><\/p>\n<p>We are used to ugly workarounds such as putting an include guard around your header. Adding the include guard\u00a0<span style=\"font-family: courier new, courier;\">FUNC_H<\/span> to the header file <span style=\"font-family: courier new, courier;\">header.h<\/span> solves the issue.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ header.h<\/span>\n\n<span style=\"color: #009999;\">#ifndef FUNC_H<\/span>\n<span style=\"color: #009999;\">#define FUNC_H<\/span>\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">func<\/span>(){}\n\n<span style=\"color: #009999;\">#endif<\/span>\n<\/pre>\n<\/div>\n<p><strong>In contrast, identical symbols with modules are very unlikely.<\/strong><\/p>\n<p>Before I end this post, I want to summarize the advantages of modules.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Advantages_of_Modules\"><\/span>Advantages of Modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Modules are only imported once and are literally for free.<\/li>\n<li>It makes no difference in which order you import a module.<\/li>\n<li>Identical symbols with modules are very unlikely.<\/li>\n<li>Modules enable you to express the logical structure of your code. You can explicitly specify names that should be exported or not. Additionally, you can bundle a few modules into a bigger module and provide them to your customer as a logical package.<\/li>\n<li>Thanks to modules, there is no need to separate your source code into an interface and an implementation part.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Modules promise a lot. In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/cpp20-a-first-module\">next post<\/a>, I will define and use my first module.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modules are one of the four big features of C++20: concepts, ranges, coroutines, and modules. Modules promise a lot: compile-time improvement, isolation of macros, the abolition of header files, and ugly workarounds.<\/p>\n","protected":false},"author":21,"featured_media":8397,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[443],"class_list":["post-5901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-modules"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5901"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5901\/revisions"}],"predecessor-version":[{"id":8404,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5901\/revisions\/8404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8397"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}