How to use c++ 20 modules in shared libraries ?

Michael Brunner 40 Reputation points
2024-05-10T19:22:17.17+00:00

Hello everyone,

I am currently working on a test-project where I want to use shared libraries and I would like to leverage the new C++20 modules feature. However, I am having some difficulty understanding how to properly set up my code to use C++20 modules between these shared libraries.

I have already set my compiler to use the C++20 standard, but I am unsure of the changes I need to make in my code and build system to use modules with shared libraries.

// test.modules.ixx

export module test.modules;

export void myFunction();

export class Test
{
public:
  Test();
};
// test.modules.cpp

module;

#include <iostream>

module test.modules;


void myFunction()
{
  std::cout << "hello from module";
}

Test::Test()
{
  std::cout << "init";
}

Command line options

/JMC /experimental:module /permissive- /ifcOutput "x64\Debug\" /GS /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "TESTLIB_EXPORTS" /D "_WINDOWS" /D "_USRDLL" /D "MATHLIBRARY_EXPORTS" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /std:c++latest /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\testLib.pch" /diagnostics:column 

When importing this into another project i get following errors:

// My Executable
import test.modules;

int main()
{
    std::cout << "Hello World!\n";
    myFunction();
    Test();
}
error C2230: could not find module 'test.modules'
error C3861: 'myFunction': identifier not found
error C3861: 'Test': identifier not found

enter image description here

Here are some specific questions I have:

  1. How should I structure my code to define and import modules in different shared libraries?
  2. Are there any specific compiler flags I need to use to enable modules?
  3. Are there any known issues or limitations I should be aware of when using C++20 modules with shared libraries?

Any examples, resources, or advice would be greatly appreciated. Thank you in advance for your help!

Thanks

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,754 questions
{count} votes

Accepted answer
  1. Minxin Yu 11,996 Reputation points Microsoft Vendor
    2024-05-13T02:15:31.14+00:00

    Hi, Michael Brunner

    If module is used in DLL, you need to use _declspec(dllexport) to export the symbol.

    1. In module.ixx:
    export _declspec(dllexport) void myFunction();
    export  class _declspec(dllexport) Test
    {
    ......
    }
    
    1. After building the DLL, find the test.modules.ifc file (By default, it is in the same directory as the project's obj file).
    2. In console project:

    Project properties -> C++ -> Command Line opition -> Add /reference "path to DLL project\x64\Debug\test.modules.ifc"

    1. In addition, add #include<iostream> in console project.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Michael Brunner 40 Reputation points
    2024-06-13T08:41:09.78+00:00

    Follow-Up Question on C++20 Modules in Shared Libraries:

    Hello,

    I’ve been exploring the use of C++20 modules in shared libraries and I understand the typical approach involves providing an .ifc file. However, I’m curious if there’s a way to utilize these modules without distributing the .ifc file. Could you provide guidance on how to use a DLL module in such a scenario?

    • Alternative Methods: Are there alternative methods to import modules without an .ifc file?
    • DLL Usage: How can one use the DLL module without the .ifc file in another project?
    • Build System Configuration: What changes are needed in the build system to support this usage?

    Any insights or examples would be greatly appreciated

    Thank you!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.