'Can't use iostream as module in C++20 (Visual Studio)
Can't get this code running in 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 similar question here Standard way of importing modules):
- /experimental:module
- /std:c++latest
- /EHsc
- /MD
Can anybody help me with this? Should i use Clang or GCC instead?
Solution 1:[1]
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 book's page for details and corresponding github repo).
Therefore they provide "no modules" examples which utilize the "#include" directive.
HTH.
Regards, Glenarvan
Solution 2:[2]
Using the Visual Studio 2019 non preview version:
Create an empty C++ project
Open project properties
Alt + EnterGo 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++ -> Advancedand 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
Solution 3:[3]
Modules in C++20 (Visual Studio)
If you are using “modules”
This will NOT work: 5. Go to Configuration Properties -> C/C++ -> Advanced and set the Compile As option to Compile as C++ Module Internal Partition (/internalPartition)
This will work: 5. Go to Configuration Properties -> C/C++ -> Advanced and set the Compile As option to Compile as C++ Module Code (/interface )
Solution 4:[4]
Try to do like that: import <iostream>; // import declaration
Modules help divide large amounts of code into logical parts. Modules are orthogonal to namespaces.
Example:
helloworld.cpp
export module helloworld; // module declaration
import <iostream>; // import declaration
export void hello() { // export declaration
std::cout << "Hello world!\n";
}
main.cpp
import helloworld; // import declaration
int main() {
hello();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Glenarvan |
| Solution 2 | Gass |
| Solution 3 | Charles Chan |
| Solution 4 | bogdyname |
