How to compile C++ 20 Modules Program using Visual C++ 2022 please

Following Visual C++ 2022 C++ 20 modules example program from C++ Professional 5th edition by Marc
is giving following error:

C7612 Couldnot find header iostream

1
2
3
4
5
6
7
8
9
10
  // helloworld.cpp

import <iostream>;

int main()
{
	std::cout << "Hello, World!" << std::endl;
	return 0;
}


Last edited on
I have selected C++ language standard as /std:C++20 in Visual studio C++ 2022

But the compiler is still giving iostream header not found error.
Last edited on
That should have compiled without problems in Visual Studio 2022 as long as you have set the C++ language standard to ISO C++20 Standard (/std:c++20).

There are two different places that can be set. With the offending project open:

1. Project -> Properties -> Configuration -> General -> C++ Language Standard.

2. Project -> Properties -> C/C++ -> Language -> C++ Language Standard.

Check that both locations are set the same, they should be.

Also right click the .cpp file in Solution Explorer and select Properties. Under the Advanced selection see what the Compile As option is set as. It should be Compile as C++ Module Code (/interface ).

Sometimes VS needs a kick in the butt.

1. Close the project, exit the VS IDE and reboot.

2. If that didn't work you run the VS Installer again, after closing out the VS IDE. You should see that 2022 is installed. Make sure you did install the Desktop development with C++ workload when you installed Visual Studio. It is not default installed, it has to be manually selected. Select Modify the installation and confirm you do have that workload installed. If not, install it now.

3. A last resort if the previous two steps didn't work is to repair the VS installation. With the VS Installer running under the More button there is the option to Repair the installation. Select that.

I wish I could be more helpful, but I haven't had that very rare problem since I stopped using Visual Studio 2019 and installed VS 2022.

One thing you might consider doing is setting the VS C++ language standard to default to C++20 for every new C++ project you start. Follow the instructions here (https://cplusplus.com/forum/lounge/271176/#msg1169093).

Yes, the instructions are for adding the Boost library directories to default. The same general steps are used to change the language standard.
BTW, did you install Visual Studio 2022 or Visual Studio Code?

VS 2022 requires Windows, x64. Win 7 or greater. It will not install on Linux.

The VS 2022 Mac version is seriously crippled compared to Windows version.
Many, many, many thanks, George P.

I can never thank you enough for this excellent compiler flags information.

Right clicking, source cpp file and selecting properties and
in Advanced selection Compile as C++ Module Code (/interface ) has worked.

Thanks again, for great help.

Best regards,

I have marked this issue as solved.
Last edited on
Keep that source file Advanced selection in the back of your mind when dealing with module files. You will have to modify it when dealing with C++ files that are partition files.
M'ok, let me give you an example of what I mean, an example from the "Beginning C++20" book, chapter 11. (https://www.amazon.com/gp/product/1484258835/)

4 files for this example.

1. roman.cppm (module interface unit, compiled as C++ module code) {think pre-C++20 header (.h/.hpp) file}
1
2
3
4
5
6
7
8
9
// interface file for a roman numerals module

export module roman;

import <string>;
import <string_view>;

export std::string to_roman(unsigned int i);
export unsigned int from_roman(std::string_view roman);

2. to_roman.cpp (module internal partition)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// implementation of the to_roman() function

module roman;

std::string to_roman(unsigned int i)
{
   if ( i > 3999 ) return {}; // 3999, or MMMCMXCIX, is the largest standard Roman numeral

   static const std::string ms[] { "","M","MM","MMM" };
   static const std::string cds[] { "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" };
   static const std::string xls[] { "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" };
   static const std::string ivs[] { "","I","II","III","IV","V","VI","VII","VIII","IX" };

   return ms[ i / 1000 ] + cds[ (i % 1000) / 100 ] + xls[ (i % 100) / 10 ] + ivs[ i % 10 ];
}

3. from_roman.cpp (module internal partition)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// implementation of the from_roman() function

module roman;

unsigned int from_roman(char c)
{
   switch ( c )
   {
   case 'I': return 1;    case 'V': return 5;   case 'X': return 10;
   case 'L': return 50;   case 'C': return 100; case 'D': return 500;
   case 'M': return 1000; default:  return 0;
   }
}

unsigned int from_roman(std::string_view roman)
{
   unsigned int result {};

   for ( size_t i {}, n { roman.length() }; i < n; ++i )
   {
      const auto j { from_roman(roman[ i ]) };   // integer value of the i'th roman digit

      // look at the next digit (if there is one) to know whether to add or subtract j
      if ( i + 1 == n || j >= from_roman(roman[ i + 1 ]) ) result += j; else result -= j;
   }

   return result;
}

4. Ex11_02.cpp (C++ module code)
1
2
3
4
5
6
7
8
9
10
11
12
// defining functions in module implementation files

import <iostream>;
import <string>;

import roman;

int main()
{
   std::cout << "1234 in Roman numerals is " << to_roman(1234) << '\n';
   std::cout << "MMXX in Arabic numerals is " << from_roman("MMXX") << '\n';
}
1234 in Roman numerals is MCCXXXIV
MMXX in Arabic numerals is 2020

M'ok, I "cleaned up" the look of the code a bit, I prefer a different formatting style than the book authors do. I didn't change any of the code, except for replacing std::endl with '\n'. :)

One thing to note, you may or may not receive Intellisense errors with any of the code files. The errors are not real, the code should compile without problems when the files are properly set to Compile As.

Intellisense is still experimental with modules. ::shrug::

MS/VS uses the .ixx file extension for module interface units, so far every non-MS book/online reference uses .cppm. VS recognizes .cppm as a module interface file, so I guess that is becoming the defacto C++ standard. .cppm is what I use.

I have both "Beginning C++20" and "Professional C++ (Fifth Edition)" books. The Beginning book is more suited to learning C++. The Professional book is geared towards C++ programmers looking to ramp up to C++20.
Last edited on
Topic archived. No new replies allowed.