Reputation: 81
Im trying to include a module within another module, but i can't compile due to the following error:
"Cannot build the following source files because there is a cyclic dependency between them: Module1.ixx depends on Module2.ixx depends on Module1.ixx."
Code i tried:
// Module1.ixx
export module Module1;
import Module2;
export class modClass1_
{
public:
modClass1_() {};
~modClass1_() {};
int getV() { return V; };
int getOtherModuleV() { modClass2_ mc2; return mc2.V; };
int V = 1;
};
// Module2.ixx
export module Module2;
import Module1;
export class modClass2_
{
public:
modClass2_() {};
~modClass2_() {};
int getV() { return V; };
int getOtherModuleV() { modClass1_ mc1; return mc1.V; };
int V = 2;
};
Any help/suggestion will be appreciated.
Environment: Visual Studio 2019 | MSVC-2019 | C++20 | Windows 10 Pro
Upvotes: 0
Views: 930
Reputation: 275720
Create module interface units that do not have circular dependencies.
A module interface unit is somewhat similar in purpose to a header file.
A module interface unit has export module
in it. The primary module interface unit has no partition name.
Remove the implementation of { modClass2_ mc2; return mc2.V; };
from export Module1;
primary module interface unit, do that in the module implementation unit of Module1
, and include import Module2;
there.
// Module1.ixx
export module Module1;
export class modClass1_
{
public:
modClass1_() {};
~modClass1_() {};
int getV() { return V; };
int getOtherModuleV();
int V = 1;
};
// Module1.mxx
module Module1;
import Module2;
int modClass1_::getOtherModuleV() { modClass2_ mc2; return mc2.V; };
and the circular dependency is broken.
Upvotes: 1