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
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.
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.
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.
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.
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 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 ...
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.
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.
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
/sdl-
)._DEBUG
preprocessor definition./fp:percise
in my case).According to https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=vs-2019 you need to use compiler switches
As well as configuring experimental modules support for the project.
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