且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将std :: filesystem :: path传递给函数segfaults

更新时间:2023-11-11 23:32:34

我认为问题在于Ubuntu在单个安装中混合了GCC版本.在Ubuntu上,默认的GCC是版本8,但是libstdc++.so.6库来自GCC9.对于GCC 8,std::filesystem定义位于单独的库libstdc++fs.a中,必须明确链接到该库.在GCC 9中,std::filesystem符号位于主libstdc++.so库中.由于安装了Ubuntu,因此libstdc++.so中的GCC 9符号可能会满足由GCC 8编译的代码中未定义的引用,而libstdc++fs.a应满足 的要求.由于GCC 9中的std::filesystem符号与GCC 8中的这些符号的实验版本不兼容,因此它似乎链接正常,但在运行时崩溃.

I think the problem is that Ubuntu mixes GCC versions in a single installation. On Ubuntu the default GCC is version 8, but the libstdc++.so.6 library comes from GCC 9. With GCC 8 the std::filesystem definitions are in a separate library, libstdc++fs.a, which must be linked to explicitly. In GCC 9 the std::filesystem symbols are in the main libstdc++.so library. Because of the mixed up Ubuntu installation it's possible for the GCC 9 symbols in libstdc++.so to satisfy the undefined references in code compiled with GCC 8 which should be satisfied by libstdc++fs.a. Because the std::filesystem symbols in GCC 9 are incompatible with the experimental versions of those symbols in GCC 8, it appears to link OK but crashes at runtime.

如果您确保与-lstdc++fs链接并且确保该选项位于所有目标文件之后,则它应该正确运行,例如这应该工作:

It should work correctly if you make sure you link with -lstdc++fs and make sure that option comes after all your object files, e.g. this should work:

g++ foo.o bar.o -lstdc++fs

但这将有效:

g++ -lstdc++fs foo.o bar.o

应该通过确保-lstdc++fs选项位于所有其他输入文件之后来对Ubuntu gcc-8软件包进行更新以解决此问题.有关更多详细信息,请参见 https://bugs.launchpad.net/ubuntu/+ source/gcc-8/+ bug/1824721

There is supposed to be an update to the Ubuntu gcc-8 packages to fix this, by ensuring the -lstdc++fs option comes after all the other input files. For more details see https://bugs.launchpad.net/ubuntu/+source/gcc-8/+bug/1824721

使用gcc-9进行编译也是可行的,因为使用GCC 9进行编译时,无需链接到-lstdc++fs来使用std::filesystem(对于GCC 9来说,仅需要使用std::experimental::filesystem符号).

Compiling with gcc-9 also works, because when using GCC 9 to compile there is no need to link to -lstdc++fs for std::filesystem (with GCC 9 it's only needed for std::experimental::filesystem symbols).