Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use iostream as module in C++20 (Visual Studio)

Tags:

c++

c++20

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):

  • /experimental:module
  • /std:c++latest
  • /EHsc
  • /MD

Should I use Clang or GCC instead?

like image 802
xleonch Avatar asked Nov 17 '20 14:11

xleonch


People also ask

How do I enable a module in Visual Studio?

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).

Can run CPP file in Visual Studio?

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.

How to use C++ modules with 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.

Is there a demo of Visual Studio and C++20?

There are many other demos of the latest Visual Studio and C++20 features in action too if you are interested.

Will C++20 modules ever be conforming to/Std:C++latest?

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:

Why can't I find the STD.* modules in Visual Studio?

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.


2 Answers

Using the Visual Studio 2019 non preview version:

  1. Create an empty C++ project

  2. Open project properties: Alt + Enter

  3. Go to Configuration PropertiesC/C++Language, and set the C++ Language Standard option to Preview - Features from the Latest C++

  4. In the same section, set Enable Experimental C++ Standard Library Modules to Yes (/experimental:module)

  5. Go to Configuration PropertiesC/C++Advanced and set the Compile As option to Compile as C++ Module Internal Partition (/internalPartition)

  6. 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>;
    
  7. Recompile your project

  8. Done, now everything should work fine

like image 146
NemanjaGk Avatar answered Oct 09 '22 19:10

NemanjaGk


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.

like image 4
Glenarvan Avatar answered Oct 09 '22 21:10

Glenarvan