How to hide implementation details in C++ modules?
Asked Answered
M

1

8

I am new to C++ modules. I am using Visual Studio 2022.

Let's say I am creating a DLL, and I don't want my users to see implementation details of my class. How can I achieve this in C++ modules? Mostly I am seeing examples over the Internet, implementing functions in the same file.

Can I safely say, module interfaces are similar to header files & module implementation is similar to cpp file?

Here is my example implementation, is this correct?

AnimalParent.ixx

export module Animal:Parent;

export class Animal
{
public:
    virtual void say();
};

AnimalParent.cpp

module Animal:Parent;

#include <iostream>

void Animal::say()
{
    std::cout << "I am Animal" << std::endl;
}

Animal.cat.ixx

export module Animal:Cat;

import :Parent;

export class Cat :public Animal
{
public:
    void say() override;
};

AnimalCat.cpp

module Animal:Cat;

#include <iostream>

void Cat::say()
{
    std::cout << "I am cat" << std::endl;
}

Animal.ixx

export module Animal;

export import :Parent;
export import :Cat;

Questions:

  1. Is this implementation correct?

  2. Can I safely assume that files contain export module name(.ixx extension) - similar to header files & module name - similar to respective source file?

  3. If I shift this DLL to my customer, what all should I give? My DLL and the folder containing .ixx files?

  4. How will they integrate my DLL? In the current header files system, they will refer to the header files directory in Additional include directories, and link to the lib. Here, do they have to refer to the folder containing .ixx files in Additional include directories & link the lib?

Mil answered 9/7, 2024 at 18:9 Comment(3)
I am new to C++ modules. My guess is that even seasoned C++ programmers are new to modules. Because this feature is not really mature yet on all tool chains, and it is challenging to migrate legacy C++ code bases to use C++20 modules.Lund
Whether you use modules or not, your implementation is private to the DLL. As long as you don't deploy the source code of the DLL, then your users won't see the implementation details. You have to provide class declarations in order for users to interact with the classes that the DLL exports to them, but they don't need the implementation code. So, I'm a little unclear on your actual issue.Cobbler
I'm pretty sure you can't use the same module name in ixx and cpp. It should be something like export module MyModule; in .ixx and module MyModule:Impl in .cpp.Gutbucket
J
4

question 1 & 2: Yes.

question 3.

To use module in dll, you need to export the symbol using __declspec(dllexport) .

AnimalParent.ixx

export module Animal:Parent;
export class  __declspec(dllexport)   Animal
{
public:
    virtual void say();
};

Animal.cat.ixx

export module Animal:Cat;

import :Parent;
export class __declspec(dllexport)  Cat :public Animal
{
public:
    void say() override;
};

what all should I give

  1. DLL and Lib files.
  2. ModuleName.ixx.ifc files. These files are generated by Visual Studio after building the DLL, similar to compiled header files. You can find them in the obj folder of your project (vcxproj project folder/x64/debug or release/).

For your project, there are three files: AnimalParent.ixx.ifc, Animal.cat.ixx.ifc, Animal.ixx.ifc.

question 4: how to use the module

1.Link the lib file.

2.Import ifc files: In project properties-> C/C++ -> Command Line -> Additional options:

/reference "<path> \AnimalParent.ixx.ifc" 
/reference "<path> \Animal.cat.ixx.ifc," 
/reference "<path> \Animal.ixx.ifc" 

Code: C++ 20 standard

import Animal;
int main()
{
    Animal Ani;
    Ani.say();
    Cat cat;
    cat.say();
}
Johnstone answered 10/7, 2024 at 1:59 Comment(4)
I don't have any references at hand, but I could have sworn I that the intermediate files are not intended to be distributed. At least for gcc and clang the guidance was to distribute the interface files (ixx in this case). Maybe it's different for msvc and distributing the ifcs is fine?Witherite
No MSDN document found related to distributing ifc files or ixx. I found a report: developercommunity.visualstudio.com/t/….Johnstone
@MinxinYu-MSFT thanks for giving detailed answer . I like this answer , but as Wutz mentioned is distributing .ifc files are fine ?Mil
As of now it seems this is the only way. Accepting this as solution. Thanks for the answer. @MinxinYu-MSFT.Mil

© 2022 - 2025 — McMap. All rights reserved.