Thread: How am I gonna create modules if they depend on each other?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    138

    How am I gonna create modules if they depend on each other?

    Rant (sorry I'm angry not because of the modules thing): Ok so first of all someone please tell me why the ........ they can't make a simple module system that works like any other ........ing language and they have to make this complicated and stupid MESS???

    Actual post:
    Ok so I have the following files:
    Code:
    // Filename: main.cc
    export module main;
    
    import second;
    import third;
    
    int main() { 
      second_fn();
      return 0;
    }
    Code:
    // Filename: second.cc
    export module second;
    
    import third;
    
    export void second_fn() {
      third_fn();
    }
    Code:
    // Filename: third.cc
    export module third;
    
    import second;
    
    #include   <stdio.h>
    
    export void third_fn() {
      printf("A message!\n");
    }
    I have read a lot of post (including the official docs from the LLVM) but nothing worked for me. Let's say that I try to compile "main.cc". I can use something like the following command:

    Code:
    clang-12 -std=c++2a -fimplicit-modules -fimplicit-module-maps -c main.cc -Xclang -emit-module-interface -o main.pcm
    But I will get the following error:

    Code:
    main.cc:3:8: fatal error: module 'second' not found
    import second;
    ~~~~~~~^~~~~~
    1 error generated.
    Ok so the module "second" must first get created. So let's first compile this file using the same command but changing the filename. Then I get this error:

    Code:
    
    second.cc:3:8: fatal error: module 'third' not found
    import third;
    ~~~~~~~^~~~~
    1 error generated.
    *STARTING TO GET ANGRY* Ok.... so let's first compile "third.cc".....

    Code:
    third.cc:3:8: fatal error: module 'second' not found
    import second;
    ~~~~~~~^~~~~~
    1 error generated.
    *THROWS COMPUTER OF THE WINDOW*

    I'm interested about GCC and Clang. Please help it's the only problem I'm having with C++ and I don't want to go on the D side because there are other problems here too....
    Last edited by rempas; 12-15-2021 at 01:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I decided to give it a go with clang 12 and ended up with:
    Code:
    // main.cc
    import second;
    
    int main()
    {
        second_fn();
    }
    Code:
    // second.cc
    export module second;
    
    import third;
    
    export void second_fn() {
        third_fn();
    }
    
    export const char* fourth_fn() {
        return "hello world";
    }
    Code:
    // third.cc
    module;
    
    #include <stdio.h>
    
    const char* fourth_fn();
    
    export module third;
    
    export void third_fn() {
        printf("A message: %s!\n", fourth_fn());
    }
    To compile, I ran:
    Code:
    clang++-12 -std=c++20 -o third.pcm -c third.cc -fprebuilt-module-path=. -Xclang -emit-module-interface
    clang++-12 -std=c++20 -o second.pcm -c second.cc -fprebuilt-module-path=. -Xclang -emit-module-interface
    clang++-12 -std=c++20 -fprebuilt-module-path=. main.cc second.cc third.cc -o main
    Then I ran ./main for a correct output of "A message: hello world!".

    The solution is not a great one though: it forward declares fourth_fn, which is presumably precisely what we are trying to avoid by switching from headers to modules in the first place.

    I've read an article confirming that cyclic imports are not permitted. There could be the argument that perhaps the second and third modules are so tightly coupled with the mutual dependency that they should just be one module, or perhaps fourth_fn should be pulled out into its own module (which arguably is the best design here if you cannot combine modules since there isn't say, mutual recursion).
    Last edited by laserlight; 12-15-2021 at 08:10 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by laserlight View Post
    The solution is not a great one though: it forward declares fourth_fn, which is presumably precisely what we are trying to avoid by switching from headers to modules in the first place.
    Yeah exactly, this is what I'm talking about.

    Quote Originally Posted by laserlight View Post
    I've read an article confirming that cyclic imports are not permitted. There could be the argument that perhaps the second and third modules are so tightly coupled with the mutual dependency that they should just be one module, or perhaps fourth_fn should be pulled out into its own module (which arguably is the best design here if you cannot combine modules since there isn't say, mutual recursion).
    Then what's the point of having modules if the system is so stupid? I mean... I know that you probably can't answer this question as you were not the one design the system but still I'm really wondering what's the idea behind them...
    Last edited by rempas; 12-15-2021 at 11:24 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rempas
    Then what's the point of having modules if the system is so stupid? I mean... I know that you probably can't answer this question as you were not the one design the system but still I'm really wondering what's the idea behind them...
    On one hand, you can say that this is the classic design-by-committee issue: from what I understand, the C++ standards committee has been debating the module feature for the better part of the last decade. Supposedly they almost included it in C++17, but changed their mind, and perhaps they couldn't agree on how to deal with this mutual dependency issue, but decided that it was not such a blocker for them to delay again, so they included modules in C++20.

    On the other hand, it is true that this mutual dependency issue isn't that much of a blocker. If you're designing a system with two components that are so tightly coupled with a mutual dependency that you cannot extract out the commonality into a third component that they can separately depend on, then that suggests that the two components should really be one component with high cohesion between the subcomponents instead. From this perspective, there's no mutual dependency issue with the current C++ modules feature; rather there is a mutual dependency/tight coupling issue with how you're structuring your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I make one project in VS depend on another
    By indigo0086 in forum Tech Board
    Replies: 19
    Last Post: 05-26-2009, 06:10 AM
  2. Create program that accept modules
    By rluceac in forum C++ Programming
    Replies: 16
    Last Post: 04-11-2009, 03:11 PM
  3. Is what I think is gonna happen gonna happen
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 10:34 PM
  4. Complicated member objects that depend on `this`
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 09:48 PM
  5. Anyone else had this e mail ??? ooh i'm gonna be rich !!
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-11-2002, 11:12 AM

Tags for this Thread