Implement module (.cppm) in separate file (MSVS2019 Community, c++20)
I've got compilation errors when I am trying to implement module in separate file. However compilation is successful when I implement module in .cppm file. What's wrong here? Is it due to experimental features or I should configure something in project's properties?
C++
-
Jeanine Zhang-MSFT 9,181 Reputation points • Microsoft Vendor
2021-08-24T05:35:10.387+00:00 According to the Doc:
You can create a module with a single interface file (.ixx) that exports names and includes implementations of all functions and types. You can also put the implementations in one or more separate implementation files, similar to how .h and .cpp files are used. The export keyword is used in the interface file only. An implementation file can import another module, but cannot export any names. Implementation files may be named with any extension. An interface file and the set of implementation files that back it are treated as a special kind of translation unit called a module unit. A name that is declared in any implementation file is automatically visible in all other files within the same module unit.
For larger modules, you can split the module into multiple module units called partitions. Each partition consists of an interface file backed by one or more implementation files. (As of Visual Studio 2019 version 16.2, partitions are not yet fully implemented.)
I suggest you could try to use vs2022 preview.
Could you please provide a sample to help us reproduce the issue?
-
Fakhriyor Otamirzaev 1 Reputation point
2021-08-24T06:55:59.293+00:00 @Jeanine Zhang-MSFT
Thank you for quick reply!There is some code in attachements to test it out (Gregoire M. - Professional C++ book). It's not production code for sure. I'm sorry about that. But it's quite enough to reproduce errors.
125869-airlineticket-cpp.txt125840-airlineticket-cppm.txt125926-airlinetickettest-cpp.txt
-
Jeanine Zhang-MSFT 9,181 Reputation points • Microsoft Vendor
2021-08-25T07:09:49.437+00:00 I test the code you provided. I couldn't get any errors. (visual studio 2019 16.10.4)
Could you please provide the error? Which version of VS are you using
-
Fakhriyor Otamirzaev 1 Reputation point
2021-08-25T09:23:44.857+00:00 Well, I think I should revise project's properties once more. Definitely I did something wrong there.
MSVS 2019 16.11.1 Community
-
Fakhriyor Otamirzaev 1 Reputation point
2021-08-25T09:36:17.69+00:00 Current project configurations contain following flags regarding to 20th standard:
C/C++ -> Language -> C++ Language Standard -> Preview - Features from the Latest C++ Working Draft (/std:c++latest)
C/C++ -> Language -> Enable Experimental C++ Standard Library Modules -> Yes(/experimental:module)
C/C++ -> Advanced -> Compile As -> Compile as C++ Module Code (/interface)
Did I forget to set another one? -
Fakhriyor Otamirzaev 1 Reputation point
2021-08-25T11:22:55.337+00:00 I tried to replace module keyword with import in implementation file (airlineticket.cpp). And build completed successfully. Is this equivalent?
-
Jeanine Zhang-MSFT 9,181 Reputation points • Microsoft Vendor
2021-08-26T06:56:28.563+00:00 /interface
: Tells the compiler that the input file is a module interface unit.The project build will automatically scan the Modules and Header Unit files (according to their Compile As setting).
As far as I'm concerned, you could use
Default
directly.Or you could modify the compile as attribute of the airlineticket.cpp.
For more details about import-keyword, module-keyword and, export-keyword, I suggest you could refer to the thread:
https://stackoverflow.com/questions/65047031/c-import-keyword-module-keyword-and-export-keyword
-
Fakhriyor Otamirzaev 1 Reputation point
2021-08-26T07:45:50.973+00:00 Tried to set Default flag and I got lot of errors again. module still causes errors too. So I decided to stay happy with my settings. Anyway thank you for your help and let's close this issue.
Sign in to comment