{"id":8496,"date":"2023-10-23T07:43:16","date_gmt":"2023-10-23T07:43:16","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=8496"},"modified":"2023-10-23T07:43:17","modified_gmt":"2023-10-23T07:43:17","slug":"c20-more-details-about-module-support-of-the-big-three","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c20-more-details-about-module-support-of-the-big-three\/","title":{"rendered":"C++20: More Details about Module Support of the Big Three"},"content":{"rendered":"\n<p>In my last post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-module-support-of-the-big-three-compilers\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/c20-module-support-of-the-big-three-compilers\/\">C++20: Module Support of the Big Three<\/a>&#8220;, I compiled a simple module with the Big Three. Today,  I drill deeper using the GCC, Clang, and Microsoft Compiler.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"390\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2-1030x390.png\" alt=\"\" class=\"wp-image-8500\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2-1030x390.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2-300x114.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2-768x291.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2-705x267.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2-845x321.png 845w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/TimelineCpp20-2.png 1077w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>Honestly, this post will be pretty technical and end with a curious note. Additionally, it requires basic modules knowledge. If you don&#8217;t have it, read my previous post about modules:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/bit.ly\/cpp20-module\">The Advantages of Modules<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/bit.ly\/SimpleMathModul\">A Simple math Modul<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/bit.ly\/ModuleInterfaceUnitModuleImplementationUnit\">Module Interface Unit and Module Implementation Unit<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/bit.ly\/DivideModules\">Structure Modules<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/bit.ly\/ModulesOpenQuestions\">Open Questions to Modules<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-modules-private-module-fragment-and-header-units\">Private Module Fragment and Header Units<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-module-support-of-the-big-three-compilers\/\">C++20: Module Support of the Big Three<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Compiler Support of Modules<\/h2>\n\n\n\n<p>I use the Microsofts cl.exe 19.29.20133 for x64, the Clang 16.0.5, and the GCC 11.1.0 compiler for my experiments. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Microsoft Visual Compiler<\/h3>\n\n\n\n<p>Let me start with the Microsoft Compiler.<\/p>\n\n\n\n<p>The Microsoft Visual Compiler provides various options for the using of modules.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"749\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Windows-1.png\" alt=\"\" class=\"wp-image-8504\" style=\"width:600px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Windows-1.png 800w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Windows-1-300x281.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Windows-1-768x719.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Windows-1-705x660.png 705w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p> Additionally, there are a few common<code> cl.exe<\/code> compiler options.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"284\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/cl.exe_.png\" alt=\"\" class=\"wp-image-8505\" style=\"width:500px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/cl.exe_.png 779w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/cl.exe_-300x109.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/cl.exe_-768x280.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/cl.exe_-705x257.png 705w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p>I use various compiler options for the ifc file in the following command lines. The ifc file is the module and contains the metadata description of the module interface.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the module <code>math.cppm<\/code> to create the obj and ifc file.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">cl.exe <span style=\"color: #555555\">\/<\/span>c <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest <span style=\"color: #555555\">\/<\/span>interface <span style=\"color: #555555\">\/<\/span>TP math.cppm\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the module <code>math.cppm<\/code> to create only the ifc file.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">cl.exe <span style=\"color: #555555\">\/<\/span>c <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest <span style=\"color: #555555\">\/<\/span>ifcOnly <span style=\"color: #555555\">\/<\/span>interface <span style=\"color: #555555\">\/<\/span>TP math.cppm\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the module <code>math.cppm<\/code> to create the obj file <code>math.obj<\/code> and the ifc file <code>mathematic.ifc<\/code>.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">cl.exe <span style=\"color: #555555\">\/<\/span>c <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest <span style=\"color: #555555\">\/<\/span>interface <span style=\"color: #555555\">\/<\/span>TP math.cppm <span style=\"color: #555555\">\/<\/span>ifcOutput mathematic.ifc\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create the executable <code>client.exe<\/code> and use the ifc file <code>math.inter<\/code>.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">cl.exe <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest client.cpp math.obj <span style=\"color: #555555\">\/<\/span>reference math.inter\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create the executable <code>client.exe <\/code>and explicitly use the ifc file <code>math.inter<\/code> that is in the directory <code>ifcFiles<\/code>.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">cl.exe <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest client.cpp math.obj <span style=\"color: #555555\">\/<\/span>ifcSearchDir ifcFiles <span style=\"color: #555555\">\/<\/span>reference math.inter\n<\/pre><\/div>\n\n\n\n<p>Let&#8217;s continue with the Clang compiler.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Clang Compiler<\/h3>\n\n\n\n<p>The Clang compiler provides various options for the creation of modules.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"952\" height=\"879\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Clang.png\" alt=\"\" class=\"wp-image-8511\" style=\"width:650px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Clang.png 952w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Clang-300x277.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Clang-768x709.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/Clang-705x651.png 705w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/figure>\n\n\n\n<p>For more details, refer to the official <a href=\"https:\/\/clang.llvm.org\/docs\/StandardCPlusPlusModules.html\" data-type=\"link\" data-id=\"https:\/\/clang.llvm.org\/docs\/StandardCPlusPlusModules.html\">Standard C++ Modules<\/a> documentation. In the following command lines, I use the compiler options for the module and the ifc file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the module declaration file<code> math.cppm<\/code> to create the pcm file <code>math.pcm<\/code>.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">clang<span style=\"color: #555555\">++<\/span> <span style=\"color: #555555\">-<\/span>c <span style=\"color: #555555\">-<\/span>std<span style=\"color: #555555\">=<\/span>c<span style=\"color: #555555\">++<\/span><span style=\"color: #FF6600\">20<\/span> <span style=\"color: #555555\">-<\/span>fmodule<span style=\"color: #555555\">-<\/span>output math.cppm <span style=\"color: #555555\">-<\/span>o math.pcm\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the module with the extension ixx (<code>math.ixx<\/code>) to create the pcm file <code>math.pcm<\/code>.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><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\">20<\/span> <span style=\"color: #555555\">--<\/span>precompile <span style=\"color: #555555\">-<\/span>x c<span style=\"color: #555555\">++-<\/span>module math.ixx <span style=\"color: #555555\">-<\/span>o math.pcm\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create the pcm file and use it.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><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\">20<\/span> <span style=\"color: #555555\">-<\/span>c math.pcm <span style=\"color: #555555\">-<\/span>o math.o\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\">20<\/span> <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 client.cpp <span style=\"color: #555555\">-<\/span>o client.exe\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the pcm file <code>other.pcm<\/code> and compile it.<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><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\">20<\/span> <span style=\"color: #555555\">-<\/span>c client.cpp <span style=\"color: #555555\">-<\/span>fmodule<span style=\"color: #555555\">-<\/span>file<span style=\"color: #555555\">=<\/span>math<span style=\"color: #555555\">=<\/span>other.pcm <span style=\"color: #555555\">-<\/span>o client.o\n<\/pre><\/div>\n\n\n\n<p>Finally, here is the GCC compiler.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">GCC Compiler<\/h3>\n\n\n\n<p>The following table shows the few GCC options.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"853\" height=\"306\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/gcc-1.png\" alt=\"\" class=\"wp-image-8512\" style=\"width:550px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/gcc-1.png 853w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/gcc-1-300x108.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/gcc-1-768x276.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/gcc-1-705x253.png 705w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/figure>\n\n\n\n<p>Many options of the Big Three are about header units. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Header Units<\/h3>\n\n\n\n<p>Header units are a binary representation of header files and represent a transition from headers to modules. You must replace the #<code>include <\/code>directive with the new <code>import <\/code>statement and add a semicolon (<code>;<\/code>).<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #009999\">#include &lt;vector&gt;      =&gt; import &lt;vector&gt;;<\/span>\n<span style=\"color: #009999\">#include &quot;myHeader.h&quot;  =&gt; import &quot;myHeader.h&quot;; <\/span>\n<\/pre><\/div>\n\n\n\n<p>For more information about header units, read my previous post &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-20-modules-private-module-fragment-and-header-units\">Private Module Fragment and Header Units<\/a>&#8220;. In the following lines, I play with header units and use the following files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The header file<code> head.h<\/code><\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ head.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>();\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The source file <code>head.cpp<\/code> importing the header unit<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ head.cpp<\/span>\n\nimport <span style=\"color: #CC3300\">&quot;head.h&quot;<\/span>;\n\n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">hello<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Hello World: header units<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The main program <code>helloWorld3.cpp<\/code> importing the header unit<\/li>\n<\/ul>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ helloWorld3.cpp<\/span>\n\nimport <span style=\"color: #CC3300\">&quot;head.h&quot;<\/span>;\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    hello();\n\n}\n<\/pre><\/div>\n\n\n\n<p>I will create a header unit from the header file <code>head.h for<\/code> the Microsoft Visual Compiler and the GCC Compiler. In contrast to the official documentation <a href=\"https:\/\/clang.llvm.org\/docs\/StandardCPlusPlusModules.html\">Standard C++ Modules<\/a>, I could not master header units with the Clang Compiler.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Microsoft Visual Compiler<\/h4>\n\n\n\n<p>These are the necessary steps to use header units.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\">cl.exe <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest <span style=\"color: #555555\">\/<\/span>EHsc <span style=\"color: #555555\">\/<\/span>exportHeader head.h\ncl.exe <span style=\"color: #555555\">\/<\/span>c <span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">:<\/span>c<span style=\"color: #555555\">++<\/span>latest <span style=\"color: #555555\">\/<\/span>EHsc <span style=\"color: #555555\">\/<\/span>headerUnit head.h<span style=\"color: #555555\">=<\/span>head.h.ifc head.cpp\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>EHsc <span style=\"color: #555555\">\/<\/span>headerUnit head.h<span style=\"color: #555555\">=<\/span>head.h.ifc helloWorld3.cpp head.obj\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The flag <code>\/exportHeader<\/code> in line 1 causes the creation of the ifc file <code>head.h.ifc<\/code> from the header file <code>head.h<\/code>.<\/li>\n\n\n\n<li>The implementation file<code> head.cpp<\/code> (line 2) and the client file<code> helloWordl3.cpp<\/code> (line 3) use the header unit. The flag <code>\/headerUnit head.h=head.h.ifc<\/code> imports the header unit and tells the compiler the name of the ifc file for the specified header.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"347\" height=\"149\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/helloWorld3.png\" alt=\"\" class=\"wp-image-8516\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/helloWorld3.png 347w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/helloWorld3-300x129.png 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">GCC Compiler<\/h4>\n\n\n\n<p>Creating and using the module consists of three steps.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"> g<span style=\"color: #555555\">++<\/span> <span style=\"color: #555555\">-<\/span>fmodules<span style=\"color: #555555\">-<\/span>ts <span style=\"color: #555555\">-<\/span>fmodule<span style=\"color: #555555\">-<\/span>header head.h <span style=\"color: #555555\">-<\/span>std<span style=\"color: #555555\">=<\/span>c<span style=\"color: #555555\">++<\/span><span style=\"color: #FF6600\">20<\/span>\n g<span style=\"color: #555555\">++<\/span> <span style=\"color: #555555\">-<\/span>fmodules<span style=\"color: #555555\">-<\/span>ts <span style=\"color: #555555\">-<\/span>c  <span style=\"color: #555555\">-<\/span>std<span style=\"color: #555555\">=<\/span>c<span style=\"color: #555555\">++<\/span><span style=\"color: #FF6600\">20<\/span> head.cpp \n g<span style=\"color: #555555\">++<\/span> <span style=\"color: #555555\">-<\/span>fmodules<span style=\"color: #555555\">-<\/span>ts  <span style=\"color: #555555\">-<\/span>std<span style=\"color: #555555\">=<\/span>c<span style=\"color: #555555\">++<\/span><span style=\"color: #FF6600\">20<\/span> head.o helloWorld3.cpp <span style=\"color: #555555\">-<\/span>o helloWorld3\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Line 1 creates the module <code>head.gcm<\/code>. The flag <code>-fmodule-header <\/code>specifies the creation of a header unit from the header  <code>head.h<\/code>.<\/li>\n\n\n\n<li>The following line creates the object file <code>head.o<\/code>.<\/li>\n\n\n\n<li>Finally, line 3 creates the executable that implicitly refers to the module <code>head.gcm<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>As promised, here is a curious note.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reachability versus Visibility<\/h2>\n\n\n\n<p>With modules, you have to distinguish between reachability and visibility. When a module exports some entity, an importing client can see and use it. Non-exported entities are not visible but may be reachable.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ bar.cppm<\/span>\n\nmodule;\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">export<\/span> module bar;\n\n<span style=\"color: #006699; font-weight: bold\">struct<\/span> Foo {\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> writeName() {\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">Foo<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    }\n\n};\n\n<span style=\"color: #006699; font-weight: bold\">export<\/span> <span style=\"color: #006699; font-weight: bold\">struct<\/span> Bar {\n    Foo getFoo() {\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> Foo{};\n    }\n};\n<\/pre><\/div>\n\n\n\n<p>The module <code>bar <\/code>exports the class <code>Bar<\/code>. <code>Bar <\/code>is visible and reachable. On the contrary, <code>Foo <\/code>is not visible.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ bar.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;utility&gt;<\/span>\n\nimport bar;\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    Bar b;\n    <span style=\"color: #0099FF; font-style: italic\">\/\/ Foo f;                   \/\/ (1)          <\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> f <span style=\"color: #555555\">=<\/span> b.getFoo();   \n    f.writeName();              <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> FooAlias <span style=\"color: #555555\">=<\/span> decltype(std<span style=\"color: #555555\">::<\/span>declval<span style=\"color: #555555\">&lt;<\/span>Bar<span style=\"color: #555555\">&gt;<\/span>().getFoo());   <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    FooAlias f2;                <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n    f2.writeName();             <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n\n}\n<\/pre><\/div>\n\n\n\n<p>The class <code>Foo<\/code> is not exported and, therefore, not visible. Its usage in line (1) would cause a linker error. On the contrary, <code>Foo<\/code> is reachable because the member function <code>getFoo<\/code> (in <code>bar.cppm<\/code>) returns it. Consequentially, the function <code>writeName<\/code> (line 2) can be invoked. Furthermore, I can create a type alias to <code>Foo<\/code> (line 3), use it to instantiate <code>Foo<\/code> (line 4), and invoke <code>writeName<\/code> (line 5) on it. The expression <code>std::declval&lt;Bar&gt;().getFoo()<\/code> (line 3) returns the object that a call <code>Bar.getFoo()<\/code> would return. Finally, <code>decltype<\/code> returns the type of this hypothetical object.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"210\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/bar.png\" alt=\"\" class=\"wp-image-8523\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/bar.png 466w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/10\/bar-300x135.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next? <\/h2>\n\n\n\n<p>In my next post, I will dive deeper into the ranges library in C++20.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post, &#8220;C++20: Module Support of the Big Three&#8220;, I compiled a simple module with the Big Three. Today, I drill deeper using the GCC, Clang, and Microsoft Compiler. Honestly, this post will be pretty technical and end with a curious note. Additionally, it requires basic modules knowledge. If you don&#8217;t have it, [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":8500,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[443],"class_list":["post-8496","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\/8496","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=8496"}],"version-history":[{"count":24,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8496\/revisions"}],"predecessor-version":[{"id":8537,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/8496\/revisions\/8537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8500"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=8496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=8496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=8496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}