c – 将std :: filesystem :: path传递给函数段错误
作者:互联网
当我尝试使用std :: filesystem :: path作为函数参数时,它会在我的机器上发生段错误.这是一个最小的例子:
#include <filesystem>
void thing(const std::filesystem::path& p) {
return;
}
int main() {
thing("test");
return 0;
}
此代码段会导致以下来自gdb的回溯:
#0 0x0000563a5a3814b3 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (this=0x23, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/stl_vector.h:567
#1 0x0000563a5a38132c in std::filesystem::__cxx11::path::~path (this=0x3, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/fs_path.h:208
#2 0x0000563a5a381f74 in std::filesystem::__cxx11::path::_Cmpt::~_Cmpt (this=0x3, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/fs_path.h:643
#3 0x0000563a5a381f8f in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt> (__pointer=0x3) at /usr/include/c++/8/bits/stl_construct.h:98
#4 0x0000563a5a381e3f in std::_Destroy_aux<false>::__destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0) at /usr/include/c++/8/bits/stl_construct.h:108
#5 0x0000563a5a381ab0 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*> (__first=0x3, __last=0x0) at /usr/include/c++/8/bits/stl_construct.h:137
#6 0x0000563a5a3817c1 in std::_Destroy<std::filesystem::__cxx11::path::_Cmpt*, std::filesystem::__cxx11::path::_Cmpt> (__first=0x3, __last=0x0) at /usr/include/c++/8/bits/stl_construct.h:206
#7 0x0000563a5a3814c9 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (this=0x7ffd198df8a0 = {...}, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/stl_vector.h:567
#8 0x0000563a5a38132c in std::filesystem::__cxx11::path::~path (this=0x7ffd198df880<error reading variable: Cannot access memory at address 0x2b>, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/fs_path.h:208
#9 0x0000563a5a381247 in main () at /home/user/CLionProjects/test/main.cpp:8
#10 0x00007fd6bb96ab6b in __libc_start_main (main=0x563a5a381200 <main()>, argc=1, argv=0x7ffd198df9b8, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffd198df9a8) at ../csu/libc-start.c:308
#11 0x0000563a5a38113a in _start ()
我在Ubuntu 19.10上使用GCC 8.3,我让其他人编译并运行此代码,在Windows上没有任何问题,因此它可能是libstdc中的一个错误
解决方法:
我认为问题是Ubuntu在单个安装中混合了GCC版本.在Ubuntu上,默认GCC是版本8,但libstdc .so.6库来自GCC 9.对于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中这些符号的实验版本不兼容,所以它似乎链接OK但在运行时崩溃.
如果您确保链接-lstdc fs并确保该选项出现在所有目标文件之后,它应该可以正常工作,例如:这应该工作:
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
使用gcc-9进行编译也很有效,因为当使用GCC 9进行编译时,不需要为std :: filesystem链接到-lstdc fs(使用GCC 9只需要std :: experimental :: filesystem符号).
标签:c,segmentation-fault,c17,std-filesystem 来源: https://codeday.me/bug/20191002/1841838.html