Open Quuxplusone opened 3 years ago
Oh, I found the reason here. The standard library I am using is libstdc++
. And its implementation for <iostream>
would use a static variable in the header:
...
// For construction of filebuffers for cout, cin, cerr, clog et. al.
static ios_base::Init __ioinit;
...
And static variable implies this variable wouldn't be used outside the current TU, so the module wouldn't export such variables. And this variable would be eliminated in PCM
files.
And as the comment says, __ioinit
would construct filebuffers std::cout
and so on. And since it is eliminated, std::cout
wouldn't be initialized correctly. So here is the crash.
I am wondering how should we handle this...
Yep, you'd probably need a modulemap for the standard library to deal with this. (a modulemap would classify the header as an importable header unit and I think Clang already does the right thing for static variables in modulemapped headers/importable header units)
I met an interesting bug which would happen only if iosstream and module file end with
.cppm
. Here is the reproducer:And if I compile it as:
And the generated executable would crash.
Interestingly, it wouldn't crash if we use
std::printf
instead ofstd::cout
Also, it wouldn't crash if the suffix is
cpp
instead ofcppm
. For example:Compile argument:
I am not sure if this is a bug in compiler or in runtime. It is appreciate if someone could help to confirm this.