g++c++-modules

header units and g++ -fmodule-mapper


I have created my own directory instead of the g++ default gcm.cache to keep my gcm files and use a file mm.txt to store the mapping as such:

$root /home/test    
std     
my_module  

and using the command:

g++ -stc=c++23 -fmodules test.cpp -fmodule-mapper="mm.txt"

with the "test.cpp"

export module my_module;    
import std;

It works perfectly.

However if I try to additionally map a compiled header unit called "my_headers.hpp" by updating the "mm.txt" file to:

$root /home/test/my_dir    
std     
my_module    
my_headers.hpp

and updating "test.txt" to:

export module my_module;
import std;
import "my_headers.hpp";

I get the error message:

test.cpp:3:1: fatal error: unknown compiled module interface: no such module
    3 | import "my_headers.hpp";
      | ^~~~~~
compilation terminated.

I tried to change in mm.txt file: my_header.hpp to its quoted form "my_header.hpp" and it didn't work either.

It seems normal module are mapped but header unit modules aren't.

How do you map a header unit with -fmodule-mapper ?


Solution

  • Solved by changing file in "mm.txt" :

    my_headers.hpp

    to

    ./my_headers.hpp

    I have included in this full demo the case for <gtkmm.h> and also the use of the comma directory.

    "mm.txt" file is:

    $root /home/test/my_dir   
    std 
    my_module 
    my_imported_module
    ./my_header_unit.hpp
    ./my_other_header_unit.hpp my_other_header_unit.hpp.gcm
    /usr/include/gtkmm-3.0/gtkmm.h  gtkmm.h.gcm
    

    "/home/test/my_dir" directory contains the following list of files:

    std.gcm
    my_module.gcm
    my_imported_module.gcm
    my_other_header_unit.hpp.gcm
    gtkmm.h.gcm
    

    "/home/test/my_dir/," directory (with a comma as the last sub-directory) contains the file:

    my_header_unit.hpp.gcm

    "test.cpp" file is

    export import my_module;
    import std;
    import my_imported_module;
    import "my_header_unit.hpp";
    import "my_other_header_unit.hpp";
    import <gtkmm.h>;
    

    Finally, the command is:

    g++ -std=c++23 -fmodules -fmodule-mapper="mm.txt" test.cpp