Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard way of importing modules

I am currently trying to use c++ modules in a code that should compile both on Windows (MSVC) and Linux (Clang and/or GCC).

I am currently developping in Visual Studio and used the "Standard Conformance Mode" (/permissive-) to make my code as portable as possible.

However the following code:

import std.core;
int main()
{
    std::cout << "Hello, World! haha" << std::endl;

    std::vector<int> myVec{4};

    std::map<std::string, size_t> myMap;

    return 0;
}

Can not compile with the /permissive- flag. I get the following error:

E3223 Cound not find module file "std.core" for import

error C2664: 'int _CrtDbgReport(int,const char *,int,const char *,const char *,...)': cannot convert argument 4 from 'int' to 'const char *'

I tought "std.core" might be a windows-only thing so i tried the following (i saw it in many examples) :

import <iostream>;
import <vector>;
import <map>;

But it results in the following errors:

error C7612: could not find header unit for 'PATH_TO_VS\include\iostream'

error C7612: could not find header unit for 'PATH_TO_VS\include\vector'

error C7612: could not find header unit for 'PATH_TO_VS\include\map'

Note : There are actually files named "iostream", "vector", and "map" in PATH_TO_VS\include.

Therefore i'm wondering what is the standard way of importing c++ modules ? If "import std.core" is the standard way, why doesn't it compile with /permissive- ?

I am using Visual Studio 2019 (Community) and CMake.

Edit:

Sorry i forgot to tell my compiler flags:

/experimental:module
/std:c++latest
/W4
/WX
/permissive-
/MDd
/EHsc

The code compiles without /permissive-, but does not when it is set. I can't figure out why

like image 219
Geroz1501 Avatar asked Dec 22 '19 18:12

Geroz1501


People also ask

What are the different ways of importing a module?

Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.

How many ways we can import a module?

The 4 ways to import a module Import specific things from the module: pycon from random import choice, randint. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd. Import specific things from the module and rename them as you're importing them: pycon from os.

How do I import a module into a file?

To use the module, you have to import it using the import keyword. The function or variables present inside the file can be used in another file by importing the module.

How do you import a module from a package?

Importing module from a package We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it. Now we can directly call this function.

How to use'from'to import a module?

Using from to import module You can import only a small part of the module i.e., only the required functions and variable names from the module instead of importing full code. When you want only specific things to be imported, you can make use of "from" keyword to import what you want. So the syntax is

How to import modules in Python 3?

How To Import Modules in Python 3 Checking For and Installing Modules. There are a number of modules that are built into the Python Standard Library,... Importing Modules. To make use of the functions in a module, you’ll need to import the module with an import statement. Using from … import. To ...

How to import a module in AutoCAD?

Description. To import a module, use the Name, Assembly, ModuleInfo, MinimumVersion and RequiredVersion parameters to identify the module to import. By default, Import-Module imports all members that the module exports, but you can use the Alias, Function, Cmdlet, and Variable parameters to restrict the members that are imported.

How do I dynamically import a module in a form?

To dynamically import a module, the import keyword may be called as a function. When used this way, it returns a promise. import('/modules/my-module.js').then((module) => { }); This form also supports the await keyword.


2 Answers

According to Microsoft Docs, import headers are not yet implemented. See https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#imported-header-files.

You can following the progress on this feature here: https://github.com/microsoft/STL/issues/60.

You can use the import std.core; syntax in Visual Studio 2019 (I tested this with v16.8+) but you will also need to install the "C++ Modules for v142 build tools" component in the Visual Studio Installer for this to work.

In addition, you will need to enable the following flags:

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

As stated in this answer.

You may still get some C5050 warnings about incompatible environment while importing the std.core module:

1>C:\Test\C++\Modules\main.cpp(1,16): warning C5050: Possible incompatible environment while importing module 'std.core': _GUARDOVERFLOW_CRT_ALLOCATORS=1 is defined in current command line and not in module command line
1>C:\Test\C++\Modules\main.cpp(1,16): warning C5050: Possible incompatible environment while importing module 'std.core': _DEBUG is defined in current command line and not in module command line
1>C:\Test\C++\Modules\main.cpp(1,16): warning C5050: Possible incompatible environment while importing module 'std.core': _M_FP_PRECISE is defined in current command line and not in module command line
  1. To solve the first warning change SDL Checks to No (/sdl-).
  2. To solve the second warning, remove the _DEBUG preprocessor definition.
  3. To solve the third warning, delete the value of the Floating Point Model (which was, by default, set to /fp:percise in my case).
like image 166
Jeremiah van Oosten Avatar answered Oct 17 '22 22:10

Jeremiah van Oosten


According to https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=vs-2019 you need to use compiler switches

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

As well as configuring experimental modules support for the project.

like image 45
KarlM Avatar answered Oct 17 '22 22:10

KarlM