How do I import a module from another project within the same Visual Studio solution?

132 views Asked by At

I'm new to C++ modules. I have a project MyProject which includes a module definition Model.ixx:

export module Model;
export import :Element;

And a partition in Element.ixx:

#include <string>
export module Model:Element;

export class Element {
private:
    std::string m_name;
public:
    Element(std::string name) {
        m_name = name;
    }

    std::string getName() {
        return m_name;
    }

    void setName(std::string name) {
        m_name = name;
    }
};

I've also got another project in the same solution, MyProjectTest, which has MyProject added as a reference and which has one file, MyProjectTest.cpp:

#include "pch.h"
#include "CppUnitTest.h"

import Model;

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace GsnEdTest
{
    TEST_CLASS(GsnEdTest)
    {
    public:
        
        TEST_METHOD(TestAttributeInit)
        {
            std::string name = "Test";
            Element e(name);
            Assert::AreEqual(name, e.getName());
        }
    };
}

But compilation of that last file fails because it cannot find the module 'Model'. How do I make it visible?

1

There are 1 answers

1
Minxin Yu - MSFT On BEST ANSWER

You need to add C++ compiler addition options for Model.ixx and Element.ixx(Unit test Project's properties-> C++ -> Command Line: additional options):

/reference "X:\\ your path \\ConsoleApp\\x64\\Debug\\Element.ixx.ifc" /reference "your path\ConsoleApp\x64\Debug\Model.ixx.ifc"

Check the usage: /reference (Use named module IFC)