How to import modules using gcc/g++ 11.1

Viewed 959

Not sure if import <module_Name>; is supported right now. When I try to run a program including modules it stats note: c++20 'import' only available with '-fmodules-ts' is that a compiler flag like -std=c++20 or are modules not currently supported. Here is an example program using modules:

#include<iostream>
import <numbers>;
import <format>;

int main()
{
    double pi {std::numbers::pi};
    std::cout << std::format("Pi is = to {}", pi);

}

I understand I can use #include <numbers> but I'm trying to figure out if modules work. I'm not so sure if <import> can be added using #include.

EDIT 10/8/21: I made a secondary program removing <format> and std::format() to test the implementation of import <numbers>; using the -fmodules-ts flag and it still didnt work. See program and terminal below.

program:

#include<iostream>
import <numbers>;


int main()
{
    double pi {std::numbers::pi};
    std::cout << pi;
}

Terminal:

g++ randomCodeWhileReading.cpp -o main -std=c++2a -fmodules-ts
randomCodeWhileReading.cpp:2:1: error: unknown Compiled Module Interface: no such module
    2 | import <numbers>;
      | ^~~~~~
In module imported at randomCodeWhileReading.cpp:2:1:
/usr/include/c++/11/numbers: error: failed to read compiled module: Unknown CMI mapping
/usr/include/c++/11/numbers: note: imports must be built before being imported
/usr/include/c++/11/numbers: fatal error: returning to the gate for a mechanical issue
compilation terminated.
1 Answers

@ExZerminator, you are right. Done with:

# g++ -std=c++20 -fmodules-ts hello.cpp -o hello.exe -x c++-system-header iostream
Related