Adelhart
Adelhart

Reputation: 415

How can I compile anything succesfully with g++, modules enabled?

I have the following file:

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 segmentation 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?

Upvotes: 7

Views: 1198

Answers (1)

Adelhart
Adelhart

Reputation: 415

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).

Upvotes: 4

Related Questions