當(dāng)我嘗試使用std :: filesystem :: path作為函數(shù)參數(shù)時,它會在我的機(jī)器上發(fā)生段錯誤.這是一個最小的例子:
#include <filesystem>
void thing(const std::filesystem::path& p) {
return;
}
int main() {
thing("test");
return 0;
}
此代碼段會導(dǎo)致以下來自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,我讓其他人編譯并運(yùn)行此代碼,在Windows上沒有任何問題,因此它可能是libstdc中的一個錯誤 解決方法: 我認(rèn)為問題是Ubuntu在單個安裝中混合了GCC版本.在Ubuntu上,默認(rèn)GCC是版本8,但libstdc .so.6庫來自GCC 9.對于GCC 8,std :: filesystem定義位于單獨(dú)的庫libstdc fs.a中,必須顯式鏈接.在GCC 9中,std :: filesystem符號位于主libstdc .so庫中.由于混合的Ubuntu安裝,libstdc .so中的GCC 9符號可能滿足用GCC 8編譯的代碼中的未定義引用,這應(yīng)該由libstdc fs.a滿足.因為GCC 9中的std :: filesystem符號與GCC 8中這些符號的實驗版本不兼容,所以它似乎鏈接OK但在運(yùn)行時崩潰.
如果您確保鏈接-lstdc fs并確保該選項出現(xiàn)在所有目標(biāo)文件之后,它應(yīng)該可以正常工作,例如:這應(yīng)該工作:
g foo.o bar.o -lstdc fs
但這不起作用:
g -lstdc fs foo.o bar.o
應(yīng)該通過確保-lstdc fs選項出現(xiàn)在所有其他輸入文件之后,對Ubuntu gcc-8軟件包進(jìn)行更新以解決此問題.有關(guān)詳細(xì)信息,請參閱https://bugs./ubuntu/ source/gcc-8/ bug/1824721
使用gcc-9進(jìn)行編譯也很有效,因為當(dāng)使用GCC 9進(jìn)行編譯時,不需要為std :: filesystem鏈接到-lstdc fs(使用GCC 9只需要std :: experimental :: filesystem符號). 來源:https://www./content-4-480401.html
|