C++20 Modules in Shared Libraries

Michael Brunner 40 Reputation points
2024-06-13T09:43:29.3+00:00

Hello,

Follow-up question to https://learn.microsoft.com/en-us/answers/questions/1665106/how-to-use-c-20-modules-in-shared-libraries

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!

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,756 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 11,996 Reputation points Microsoft Vendor
    2024-06-13T10:05:56.59+00:00

    Hi,

    After testing

    Alternative Methods:

    1. Providing forward declarations

    Since the source file has been compiled into a DLL, all you need to do is provide the necessary declarations.

    main.cpp

     void myFunction();
     class  Test;
       class Test
     {
     public:
         Test();
     };
    //import test.modules;
    int main()
    {
        myFunction();
        Test();
    }
    

    Or 2. Right click project -> Add -> Existing item: your test.modules.ixx file to the project. (test.modules.cpp file is not needed)

    import test.modules;
    int main()
    {
        myFunction();
        Test();
    }
    

    The compiler will generate an ifc file for use in your project.

    Also, don't forget to link to the DLL.

    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.


0 additional answers

Sort by: Most helpful

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.