How to compile anything succesfully with g++, modules enabled?

Viewed 172

I have the following file: Testing2.cpp:

#include <string>
#include <iostream>

int main() {
    std::string ss = "aaaa";
    ss += "aa";
    std::cout << ss << "\n";
}

When I compile it like this: g++-11 -o Testing2 Testing2.cpp -std=c++20, I get the desired output. When I compile it like this: g++-11 -o Testing2 Testing2.cpp -std=c++20 -fmodules-ts, I get a runtime segmentaion fault error.

Question 1: Is this a bug in g++, or am I missing something?

I had several similar issues, one with std::filesystem, one with std::map. The code will not work properly, even if modules are not actually used.

Question 2: The above code does not even use modules. Why does g++ compile it differently when the ``-fmodules-ts``` is enabled from when it is not enabled?

Any insight is welcome.

1 Answers

As it turns out, g++ will use the precompiled standard library modules, if available (located in the gcm.cache folder) in both of these cases: #include <string> and import <string>;.

My problem was solved by removing the string and iostream modules from the gcm.cache directory and recompiling them (I used the command g++-11 -std=c++20 -fmodules-ts -c -x c++-system-header string).

Related