Comment 6 for bug 1824721

Revision history for this message
Jonathan Wakely (jwakely) wrote :

The GCC 8 std::filesystem (and std::experimental::filesystem) symbols are in libstdc++fs.a which is only provided as a static library, so link order does matter. If you don't tell the linker to use it after the objects that require it, then no symbols from libstdc++fs.a will be used.

In GCC 9 the std::experimental::filesystem symbols are still in libstdc++fs.a but the std::filesystem symbols are in libstdc++.so.6.0.26 now, but they're incompatible with the GCC 8 definitions. In your problematic link no symbols were used from libstdc++fs.a, so the linker finds the GCC 9 ones in libstdc++.so.6.0.25 and uses them, but that leads to crashes.

It would have been possible to make the GCC 8 and 9 std::filesystem symbols mangle differently, and maybe I should do that for GCC 8.4, but for now the short answer is "C++17 support in GCC 8 is experimental, the onus is on you to link correctly".

Since this problem is specific to Ubuntu, due to mixing gcc-8 and libstdc++.so.6.0.26, another possibility would be for Ubuntu's gcc-8 to reorder the linker arguments. If -lstdc++fs is given it could be moved to after the object files (but before the implicit -lstdc++ option that g++ adds).

Another option would be to make g++ simply link to -lstdc++fs implicitly, as it does for -lstdc++. Unless the user adds -Wl,--whole-archive (and doesn't follow it with -Wl,--no-whole-archive) that should be harmless because the symbols in libstdc++fs.a will only be used if actually needed.