Unable to Import C++ Modules
Asked Answered
E

1

6

I'm trying to rebuild a project of mine in C++ 20 using modules to reduce dependencies and compilation times. I tried importing some modules, and I'm able to do so, yet Visual Studio marks the importation statements as undefined: "Could not find module file for...". Although it marks it as wrong, I'm still able to compile and run the imported functions (although it doesn't allow me to compile a function if it requires a separate #include found only in the module but not the .cpp file it imports to, giving me a linker error). I followed Microsoft's documentation closely and was able to utilize modules on an earlier project and never met any of these problems then, and I made sure to reestablish the same configurations in this project yet this happens.

I'm not really sure what other information I could provide, but please ask if any is necessary. Any help would be appreciated

Config: Config

Works

import Object;

#include <iostream>

int main() {
    calc(1, 5);
}
export module Object;

export int calc(int a, int b) {
    return a + b;
}

Doesn't Work

import Object;

#include <iostream>

int main() {
    calc(1, 5);
}
export module Object;

#include <iostream>

export int calc(int a, int b) {
    return a + b;
}

As you can see, there is multiple linking error: With include

Either way, VS marks it as an error

Edit By putting the #include above the module export statement the code compiles, but VS still marks it as an error. Although importing headers such as <iostream> or exporting classes doesn't work. So I'm able to compile, but only certain things, and VS always marks it as wrong

Euchologion answered 4/5, 2021 at 22:48 Comment(8)
You should provide an minimal reproducible example. In this case, the files you're using and the build settings.Knout
I made sure to enable the experimental module feature and to choose the latest C++ standard. The thing is, I used the exact same build settings in a project earlier today to test out modules and it worked, yet I can't seem to replicate itEuchologion
If you can't replicate the issue, that means we're not really going to be able to either, and definitely not if you don't show the setup you're using.Knout
Alright, here's the language config pageEuchologion
You need to show the files you're using as well. Just a few files with a couple of lines of code should be sufficient to replicate the error you're getting.Knout
Are you sure that the one you say works works? Your screenshot still shows a ton of linker errors.Equidistance
That's because I didn't recompiled after testing the one with the errors, but running it now it worksEuchologion
@ItsMe I edited the question to include the code. You can't search an image on google, and reading images when text is possible it's just not fun. If you can also put the compiler output of the linking error instead of the last image, that would be super great.Presentiment
P
5

You cannot include like that in a module. Including a header file into a module will make all of its content part of your module. Since you do not define the implementation for the standard library function you declare from the include, it results in linking errors.

You have two choices for headers.

The first is to use global module fragments:

module;
#include <iostream>
export module Object;

// ...

The second choices is to use header units:

export module Object;

import <iostream>
Presentiment answered 5/5, 2021 at 4:21 Comment(10)
Thank you! I was wondering what "the visual studio build system is limited in what name and structure you can have" specifically refers to. In what situation would I be able to use modules? And on another note - I tried importing rather than including headers as you suggested but I was unable to do soEuchologion
@ItsMe Hmm, reading the documentation it seems like they removed that restriction in the build system, so in theory you should be able to import any module, as long as you define them in you project. I have no idea about external dependencies though.Presentiment
@ItsMe And reading the documentation the import <header> should simply work, so I'm not sure what has gone wrong here. I never tried modules yet.Presentiment
It worked just fine on an earlier project... definitely not now though. Thank you either wayEuchologion
When I try to import, for example, std.core, it gives me this error: C1011: cannot locate... Did you install the library part of the C++ modules feature in VS setup?. Online it seems other people have encountered the problem as wellEuchologion
@ItsMe That is completely normal. The module std.core simply don't exist. There is no modules for the standard library in C++20. You have to include the old headers in the ways I showed you. The std.core module (or whatever name it will have) is expected to come in C++23Presentiment
Ok, good to know. Microsoft's documentation made it seem as if its already out, or maybe it was just how I read itEuchologion
@ItsMe That is because of the option /experimental:module option that enables the experimental modules for the standard library. In the latest version of MSVC, /std:c++latest will enable modules, without the standard library ones.Presentiment
I know this is a pretty perspective-based question, but would you recommend using modules as-is or waiting for further integration?Euchologion
That is completely opinion based and dependent on your project, and what you have to support and what tools you have. I cannot answer this question.Presentiment

© 2022 - 2024 — McMap. All rights reserved.