I tried to use C++20 Module, but it just can't work in my Visual Studio installation.
These are my configuration in Visual Studio:
https://imgur.com/5ISbdak
https://imgur.com/7kNySE5
I even installed the "C++ Modules for v142 build tools":
https://imgur.com/9gCzB3X
But VS still can't recognize the module syntax and can't compile it: https://imgur.com/5PaDAkI
My code is just simple like this:
import <iostream>;
int main() {
std::cout << "Hello World\n";
}
I googled around but it seems that only I have this problem. Can anyone help me about this?
Edit: My question is not a duplicated, here is the compiler output if it's necessary, I also tried the "Compile as C++ Module Code (/interface)" as told in a comment:
Build started...
1>------ Build started: Project: C++Modules, Configuration: Debug Win32 ------
1>Source.cpp
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(1,8): error C2143: syntax error: missing ';' before '<'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(1,8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(4,10): error C2039: 'cout': is not a member of 'std'
1>D:\Draftbook\C++Modules\C++Modules\predefined C++ types (compiler internal)(340): message : see declaration of 'std'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(4,15): error C2065: 'cout': undeclared identifier
1>Done building project "C++Modules.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Using import std.core;, the same issue:
Build started...
1>------ Build started: Project: C++Modules, Configuration: Debug Win32 ------
1>Source.cpp
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(1,11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(1,8): error C2146: syntax error: missing ';' before identifier 'std'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(4,10): error C2039: 'cout': is not a member of 'std'
1>D:\Draftbook\C++Modules\C++Modules\predefined C++ types (compiler internal)(340): message : see declaration of 'std'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(4,15): error C2065: 'cout': undeclared identifier
1>Done building project "C++Modules.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I also tried an example from https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-170
// Example.ixx
export module Example;
#define ANSWER 42
namespace Example_NS {
int f_internal() {
return ANSWER;
}
export int f() {
return f_internal();
}
}
// Source.cpp
import Example;
import std.core;
using namespace std;
int main() {
cout << "The result of f() is " << Example_NS::f() << endl;
}
Rebuild started...
1>------ Rebuild All started: Project: C++Modules, Configuration: Debug Win32 ------
1>Example.ixx
1>D:\Draftbook\C++Modules\C++Modules\Example.ixx(2,1): error C3378: a declaration can be exported only from a module interface unit
1>D:\Draftbook\C++Modules\C++Modules\Example.ixx(2,22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>D:\Draftbook\C++Modules\C++Modules\Example.ixx(2,15): error C2146: syntax error: missing ';' before identifier 'Example'
1>D:\Draftbook\C++Modules\C++Modules\Example.ixx(8,5): error C3378: a declaration can be exported only from a module interface unit
1>Source.cpp
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(2,15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(2,8): error C2146: syntax error: missing ';' before identifier 'Example'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(3,11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(3,1): error C2086: 'int import': redefinition
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(2): message : see declaration of 'import'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(3,8): error C2146: syntax error: missing ';' before identifier 'std'
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(6,5): error C2065: 'cout': undeclared identifier
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(6,40): error C2653: 'Example_NS': is not a class or namespace name
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(6,52): error C3861: 'f': identifier not found
1>D:\Draftbook\C++Modules\C++Modules\Source.cpp(6,59): error C2065: 'endl': undeclared identifier
1>Generating Code...
1>Done building project "C++Modules.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========