{"id":5673,"date":"2019-04-29T19:35:09","date_gmt":"2019-04-29T19:35:09","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-20-modules\/"},"modified":"2023-06-26T10:11:17","modified_gmt":"2023-06-26T10:11:17","slug":"c-20-modules","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-20-modules\/","title":{"rendered":"Modules"},"content":{"rendered":"<p>Modules are one of the five prominent features of C++20. Modules will overcome the restrictions of header files. They promise a lot. For example, the separation of header and source files becomes as obsolete as the preprocessor. Ultimately, we will also have faster build times and an easier way to build packages.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5671\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/graphic-3578346_1280.png\" alt=\"graphic 3578346 1280\" width=\"400\" height=\"366\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/graphic-3578346_1280.png 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/graphic-3578346_1280-300x275.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/graphic-3578346_1280-1024x938.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/graphic-3578346_1280-768x703.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Explaining modules from the user&#8217;s perspective is quite easy, but this will not hold for the implementer&#8217;s perspective. My plan for this post is to start with a simple example of modules and add more features as we go.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"A_First_Example\"><\/span>A First Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First of all, here is my first <code>math<\/code> module<span style=\"font-family: courier new, courier;\">.<\/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;\">\/\/ math.cppm<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">add<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\r\n} \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The expression <span style=\"font-family: courier new, courier;\">export module math<\/span> is the module declaration. By putting <span style=\"font-family: courier new, courier;\">export<\/span> before the function adds<span style=\"font-family: courier new, courier;\">,<\/span> <span style=\"font-family: courier new, courier;\">add <\/span>is exported and can, therefore, be used by a consumer of my module.&nbsp;<\/p>\n<p>&nbsp;<\/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;\">\/\/ main.cpp<\/span>\r\n\r\nimport math;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n   \r\n   add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>);\r\n   \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-family: courier new, courier;\">import math<\/span> imports the module <span style=\"font-family: courier new, courier;\">math<\/span> and makes the exported names in <span style=\"font-family: 'courier new', courier;\">main.cpp<\/span> visible. This was the easy part. The challenge started when I compiled the program.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Module_Declaration_Files\"><\/span>Module Declaration Files<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>But first, did you notice the strange name of the module: <span style=\"font-family: courier new, courier;\">math.cppm<\/span>.<\/p>\n<ul>\n<li>The extension&nbsp;<strong><span style=\"font-family: courier new, courier;\">cppm<\/span><\/strong> stands presumably for cpp module declaration and is the suggested extension for Clang.<\/li>\n<li>cl.exe uses the extension <strong><span style=\"font-family: courier new, courier;\">ixx. <\/span><\/strong>The <span style=\"font-family: courier new, courier;\"><strong>i<\/strong> <\/span>should stand in this case for the interface.<span style=\"font-family: courier new, courier;\"> <\/span><\/li>\n<li>I don&#8217;t know of a GCC extension.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Compile_the_Module_math\"><\/span>Compile the Module <span style=\"font-family: courier new, courier;\">math<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To compile the module, you must use a very current clang or cl.exe compiler. It is also possible to use GCC to compile the examples of this post, but I will go into this post with clang and cl.exe on Windows. Here are more details about my compilers:<\/p>\n<ul>\n<li>clang++<\/li>\n<\/ul>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5377\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/01\/clang.png\" alt=\"clang\" width=\"350\" height=\"132\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>cl.exe<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5672\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/clExe.png\" alt=\"clExe\" width=\"500\" height=\"114\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/clExe.png 1998w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/clExe-300x68.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/clExe-1024x233.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/clExe-768x175.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/04\/clExe-1536x350.png 1536w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Here is the point where the fun started: figuring out the command line for clang++ and cl.exe.<\/p>\n<p>&nbsp;<\/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%;\">clang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts <span style=\"color: #555555;\">--<\/span>precompile math.cppm <span style=\"color: #555555;\">-<\/span>o math.pcm                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\nclang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts <span style=\"color: #555555;\">-<\/span>c math.pcm <span style=\"color: #555555;\">-<\/span>o math.o                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\nclang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts <span style=\"color: #555555;\">-<\/span>fprebuilt<span style=\"color: #555555;\">-<\/span>module<span style=\"color: #555555;\">-<\/span>path<span style=\"color: #555555;\">=<\/span>. math.o main.cpp <span style=\"color: #555555;\">-<\/span>o math     <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\n\r\n\r\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>experimental<span style=\"color: #555555;\">:<\/span>module <span style=\"color: #555555;\">\/<\/span>TP <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>MD <span style=\"color: #555555;\">\/<\/span>c math.cppm <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>interface <span style=\"color: #555555;\">\/<\/span>Fo<span style=\"color: #555555;\">:<\/span> math.obj <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>output math.pcm <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>experimental<span style=\"color: #555555;\">:<\/span>module <span style=\"color: #555555;\">\/<\/span>TP <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>MD <span style=\"color: #555555;\">\/<\/span>c main.cpp <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>reference math.pcm <span style=\"color: #555555;\">\/<\/span>Fo<span style=\"color: #555555;\">:<\/span> main.obj                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\ncl.exe math.obj main.obj                                                                                                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ol>\n<li>Creates a precompiled module <span style=\"font-family: courier new, courier;\">math.pcm<\/span> out of the module declaration <span style=\"font-family: courier new, courier;\">math.cppm<\/span><\/li>\n<li>Creates the non-module translation unit <span style=\"font-family: courier new, courier;\">math.o<\/span>.&nbsp;<\/li>\n<li>Creates the executable <span style=\"font-family: courier new, courier;\">math<\/span> or <span style=\"font-family: courier new, courier;\">math.exe. <\/span>For clang++, I have to specify the module path.<\/li>\n<\/ol>\n<p>For obvious reasons, I will not show you the output of the program execution. I will do it if I have something to show.<\/p>\n<p>From the implementor&#8217;s perspective, we can split the module definition into a module interface unit and a module implementation unit. Before I come to these units, let me take a step back and answer the question:<\/p>\n<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_are_the_Advantages_of_Modules\"><\/span>What are the Advantages of Modules?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>Compile-time speedup: <\/strong>A module is only imported once and should be literally for free. Compare this with M headers which are included in N translation units. The combinatorial explosion means that the header has to be parsed M*N times.<\/li>\n<li><strong>Isolation from the preprocessor macros<\/strong>: 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++ semantic. 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. In contrast, it makes no difference in which order you import modules.<\/li>\n<li><strong>Express the logical structure of your code<\/strong>: Modules allow you to express which names should be exported or not explicitly. You can bundle a few modules into a more significant module and provide them to your customer as a logical package.<\/li>\n<li><strong>No need for header files: <\/strong>There is no need to separate your files into an interface and an implementation part. This means modules have just half the number of source files.<\/li>\n<li><strong>Get rid of ugly workarounds:<\/strong> We are used to ugly workarounds such as &#8220;put an include guard around your header&#8221;, or &#8220;write macros with LONG_UPPERCASE_NAMES&#8221;. On the contrary, identical names in modules will not clash.<\/li>\n<\/ul>\n<p>In my first module <span style=\"font-family: courier new, courier;\">math<\/span>, I declared and defined the module in one file <span style=\"font-family: courier new, courier;\">math.cppm<\/span>. Let me talk about the new units.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Module_Interface_Unit_and_Module_Implementation_Unit\"><\/span>Module Interface Unit, and Module Implementation Unit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>&nbsp;First, the new module <span style=\"font-family: courier new, courier;\">math1<\/span> consists of a module interface unit and a module implementation unit.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Module_Interface_Unit\"><\/span>Module Interface Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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;\">\/\/ math1.cppm<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> module math1;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">export<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">add<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>The module interface unit contains the exporting module declaration: <span style=\"font-family: courier new, courier;\">export module math1<\/span>.<\/li>\n<li>Names such as <span style=\"font-family: courier new, courier;\">add<\/span> can only be exported in the module interface unit.<\/li>\n<li>Names which are not exported are not visible outside the module. I will come to this point in my next post.<\/li>\n<li>A module can have only one module interface unit.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Module_Implementation_Unit\"><\/span>Module Implementation Unit<span class=\"ez-toc-section-end\"><\/span><\/h3>\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;\">\/\/ math1.cpp<\/span>\r\n\r\nmodule math1;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">add<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span> fir, <span style=\"color: #007788; font-weight: bold;\">int<\/span> sec){\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> fir <span style=\"color: #555555;\">+<\/span> sec;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>The module implementation unit contains non-exporting module declarations: <span style=\"font-family: courier new, courier;\">module math1;<\/span><\/li>\n<li>A module can have more than one module implementation unit.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Main_Program\"><\/span>Main Program<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<\/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;\">\/\/ main1.cpp<\/span>\r\n\r\nimport math1;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n   \r\n   add(<span style=\"color: #ff6600;\">2000<\/span>, <span style=\"color: #ff6600;\">20<\/span>);\r\n   \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>From the user&#8217;s perspective, just the module name changed from <span style=\"font-family: courier new, courier;\">math<\/span> to <span style=\"font-family: courier new, courier;\">math1<\/span>.<\/li>\n<\/ul>\n<p>Compiling the modularised module is a little bit more involved.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Compile_the_Module_math1\"><\/span>Compile the Module <span style=\"font-family: courier new, courier;\">math1<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;<\/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%;\">clang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts <span style=\"color: #555555;\">--<\/span>precompile math1.cppm <span style=\"color: #555555;\">-<\/span>o math1.pcm               <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\nclang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts  <span style=\"color: #555555;\">-<\/span>c math1.pcm <span style=\"color: #555555;\">-<\/span>o math1.pcm.o                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\nclang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts <span style=\"color: #555555;\">-<\/span>c math1.cpp <span style=\"color: #555555;\">-<\/span>fmodule<span style=\"color: #555555;\">-<\/span>file<span style=\"color: #555555;\">=<\/span>math1.pcm <span style=\"color: #555555;\">-<\/span>o math1.o    <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\nclang<span style=\"color: #555555;\">++<\/span> <span style=\"color: #555555;\">-<\/span>std<span style=\"color: #555555;\">=<\/span>c<span style=\"color: #555555;\">++<\/span><span style=\"color: #ff6600;\">2<\/span>a <span style=\"color: #555555;\">-<\/span>fmodules<span style=\"color: #555555;\">-<\/span>ts <span style=\"color: #555555;\">-<\/span>c main1.cpp <span style=\"color: #555555;\">-<\/span>fmodule<span style=\"color: #555555;\">-<\/span>file<span style=\"color: #555555;\">=<\/span>math1.pcm <span style=\"color: #555555;\">-<\/span>o main1.o    <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\nclang<span style=\"color: #555555;\">++<\/span>  math1.pcm main1.o math1.o <span style=\"color: #555555;\">-<\/span>o math                                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4<\/span>\r\n\r\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>experimental<span style=\"color: #555555;\">:<\/span>module <span style=\"color: #555555;\">\/<\/span>TP <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>MD <span style=\"color: #555555;\">\/<\/span>c math1.cppm <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>interface <span style=\"color: #555555;\">\/<\/span>Fo<span style=\"color: #555555;\">:<\/span> math1.pcm.obj <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>output math1.pcm  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>experimental<span style=\"color: #555555;\">:<\/span>module <span style=\"color: #555555;\">\/<\/span>TP <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>MD <span style=\"color: #555555;\">\/<\/span>c math1.cpp <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>reference math1.pcm <span style=\"color: #555555;\">\/<\/span>Fo<span style=\"color: #555555;\">:<\/span> math1.obj                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\ncl.exe <span style=\"color: #555555;\">\/<\/span>std<span style=\"color: #555555;\">:<\/span>c<span style=\"color: #555555;\">++<\/span>latest <span style=\"color: #555555;\">\/<\/span>experimental<span style=\"color: #555555;\">:<\/span>module <span style=\"color: #555555;\">\/<\/span>TP <span style=\"color: #555555;\">\/<\/span>EHsc <span style=\"color: #555555;\">\/<\/span>MD <span style=\"color: #555555;\">\/<\/span>c main1.cpp <span style=\"color: #555555;\">\/<\/span>module<span style=\"color: #555555;\">:<\/span>reference math1.pcm <span style=\"color: #555555;\">\/<\/span>Fo<span style=\"color: #555555;\">:<\/span> main1.obj                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\ncl.exe math1.obj main1.obj math1.pcm.obj                                                                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ol>\n<li>Creates a precompiled module <span style=\"font-family: courier new, courier;\">math1.pcm<\/span> out of the module declaration <span style=\"font-family: courier new, courier;\">math1.cppm<\/span><\/li>\n<li>Compiles the precompiled module <span style=\"font-family: courier new, courier;\">math1.pcm: math1.pcm.o<\/span>. Compile the source file<span style=\"font-family: courier new, courier;\"> math1.cpp: math1.o. <\/span>cl.exe does this in one step.<\/li>\n<li>Compiles the main program: <span style=\"font-family: 'courier new', courier;\">main1.o<\/span> or&nbsp;<span style=\"font-family: courier new, courier;\">main1.obj.<\/span><\/li>\n<li>Creates the executable <span style=\"font-family: courier new, courier;\">math1<\/span> or <span style=\"font-family: courier new, courier;\">math1.exe.<\/span><\/li>\n<\/ol>\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>As promised, this was only an introduction to modules. In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-more-details-to-modules\">next post<\/a>, I will dive more into the details. In particular, I want to show the program&#8217;s output and have, therefore, include standard headers such as <span style=\"font-family: courier new, courier;\">&lt;iostream&gt;<\/span> or import modules such as <span style=\"font-family: courier new, courier;\">std.core<\/span>.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modules are one of the five prominent features of C++20. Modules will overcome the restrictions of header files. They promise a lot. For example, the separation of header and source files becomes as obsolete as the preprocessor. Ultimately, we will also have faster build times and an easier way to build packages.<\/p>\n","protected":false},"author":21,"featured_media":5671,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[443],"class_list":["post-5673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-modules"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5673","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=5673"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5673\/revisions"}],"predecessor-version":[{"id":6789,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5673\/revisions\/6789"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5671"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}