I can't get this code running in the newest version of MSVC. This code example is from the book called "Beginning C++20, From Novice to Professional" by Ivor Horton and Peter Van Weert.
import <iostream>;
int main()
{
int answer {42};
std::cout << "The answer to life, universe, and everything is "
<< answer
<< std::endl;
return 0;
}
I get this error:
could not find header unit for 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\iostream'
I am using Microsoft Visual Studio version 16.8.1, and have enabled these flags in project properties (according to the similar question Standard way of importing modules):
Should I use Clang or GCC instead?
In a Visual Studio project, right-click the project node in Solution Explorer and choose Properties. Set the Configuration drop-down to All Configurations, then choose Configuration Properties > C/C++ > Language > Enable C++ Modules (experimental).
You can use Visual Studio to create Standard C++ programs. By following the steps in this walkthrough, you can create a project, add a new file to the project, modify the file to add C++ code, and then compile and run the program by using Visual Studio.
To do this, set the C++ Language Standard to “Preview /std:c++latest”. If you have multiple projects in your solution, remember to do this for all of them. And that’s it! You are ready to use C++ modules with Visual Studio.
There are many other demos of the latest Visual Studio and C++20 features in action too if you are interested.
Since the merge of Modules into the C++20 standard (we can officially say C++20 now!) the compiler has been working towards C++20 Modules conformance until precisely such a time that we can confidently roll Modules into /std:c++latest. That time is now! There are a few caveats to implying C++ Modules under /std:c++latest:
The std.* Modules which ship with Visual Studio will not be available through /std:c++latest alone. The standard library Modules have not yet been standardized and as such remain experimental. To continue using the standard library Modules users will need /experimental:module as part of their command line options.
Using the Visual Studio 2019 non preview version:
Create an empty C++ project
Open project properties: Alt + Enter
Go to Configuration Properties → C/C++ → Language, and set the C++ Language Standard option to Preview - Features from the Latest C++
In the same section, set Enable Experimental C++ Standard Library Modules to Yes (/experimental:module)
Go to Configuration Properties → C/C++ → Advanced and set the Compile As option to Compile as C++ Module Internal Partition (/internalPartition)
Add header file to your project, which contains an import declaration for every standard library header you want to import. For example:
#pragma once
import <iostream>;
import <array>;
import <vector>;
Recompile your project
Done, now everything should work fine
The authors stated in the example and solution files provided for download, that due to compilers' implementations of the C++20-standard the files might not work yet (see the book's page for details and corresponding GitHub repository).
Therefore they provide "no modules" examples which use the "#include" directive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With