Desmond Gold
Desmond Gold

Reputation: 2065

How to use modules in c++ 20 using g++?

I read this link https://gcc.gnu.org/wiki/cxx-modules and tried to copy the following example from this website. I already knew that this compiler partially supports the module system. (Note: I used windows)

// hello.cc
module;
#include <iostream>
#include <string_view>
export module hello;
export void greeter(std::string_view const &name) {
  std::cout << "Hello " << name << "!\n";
}
// main.cc
import hello;
int main() {
  greeter("world");
  return 0;
}
// Should print "Hello world!" 

I currently have GCC 11.0.1 snapshot and tried to compile these files using the following arguments: g++ -std=gnu++2b -Wall -fmodules-ts hello.cc main.cc and the compiler gave me these errors:

hello.cc:6:8: internal compiler error: in get_cxx_dialect_name, at cp/name-lookup.c:7027
    6 | export module hello;
      |        ^~~~~~
libbacktrace could not find executable to open
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://gcc.gnu.org/bugs/> for instructions.
In module imported at main.cc:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is 'gcm.cache/hello.gcm'
hello: note: imports must be built before being imported
hello: fatal error: returning to the gate for a mechanical issue
compilation terminated.

If I compile first the hello.cc, then the compiler still gave errors:

C:\...\hello.cc | 6 | error: failed to write compiled module: No such file or directory |
C:\...\hello.cc | 6 | note: compiled module file is 'gcm.cache/hello.gcm' |

Upvotes: 7

Views: 7652

Answers (1)

Ben Crowhurst
Ben Crowhurst

Reputation: 8481

I hit a similar problem when upgrading an existing project. The issue is related to the file extension, ensure you have named the compilation unit as hello.cpp.

bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.mpp main.cpp
In module imported at main.cpp:1:1:
hello: error: failed to read compiled module: No such file or directory
hello: note: compiled module file is ‘gcm.cache/hello.gcm’
hello: note: imports must be built before being imported
hello: fatal error: returning to the gate for a mechanical issue
compilation terminated.
bcrowhurst@pop-os:/tmp$ mv hello.mpp hello.cpp
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.cpp main.cpp
bcrowhurst@pop-os:/tmp$ ./a.out
Hello world!

Additionally, I hit an issue when attempting to rename the file back to hello.mpp to see the original error; removing gcm.cache/ folder fixed it.

bcrowhurst@pop-os:/tmp$ mv hello.cpp hello.mpp
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.mpp main.cpp
/usr/bin/ld:hello.mpp: file format not recognized; treating as linker script
/usr/bin/ld:hello.mpp:1: syntax error
collect2: error: ld returned 1 exit status
bcrowhurst@pop-os:/tmp$ rm -rf gcm.cache
bcrowhurst@pop-os:/tmp$ g++ -fmodules-ts hello.cpp main.cpp
bcrowhurst@pop-os:/tmp$ ./a.out
Hello world!

It appears g++-11.1 has a restriction (by design?) when scanning for module compilation units. Which the C++20 specification does not concern itself.

Links

https://en.cppreference.com/w/cpp/language/modules

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4720.pdf

Upvotes: 3

Related Questions