使用std::stacktrace遭遇编译链接错误的解决方法咨询
问题
在Fedora 39 Linux系统中使用libstdc的C23特性std::stacktrace时,最简示例出现编译/链接错误:
示例代码:
#include <stacktrace> int main() { (void)to_string( std::stacktrace::current() ); }
Clang编译错误
/opt/compiler-explorer/gcc-13.2.0/lib/gcc/x86_64-linux-gnu/13.2.0/../../../../include/c++/13.2.0/stacktrace:644:3: error: no matching function for call to 'operator delete' 644 | _GLIBCXX_OPERATOR_DELETE (static_cast<void*>(_M_frames), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 645 | _M_capacity * sizeof(value_type)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /opt/compiler-explorer/gcc-13.2.0/lib/gcc/x86_64-linux-gnu/13.2.0/../../../../include/c++/13.2.0/stacktrace:599:35: note: expanded from macro '_GLIBCXX_OPERATOR_DELETE' 599 | # define _GLIBCXX_OPERATOR_DELETE __builtin_operator_delete | ^ /opt/compiler-explorer/gcc-13.2.0/lib/gcc/x86_64-linux-gnu/13.2.0/../../../../include/c++/13.2.0/new:144:6: note: candidate function not viable: no known conversion from 'unsigned long' to 'const std::nothrow_t' for 2nd argument 144 | void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT | ^ ~~~~~~~~~~~~~~~~~~~~~ ...
GCC链接错误
/opt/compiler-explorer/gcc-13.2.0/bin/../lib/gcc/x86_64-linux-gnu/13.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/ccXWeU5s.o: in function `std::stacktrace_entry::_S_init()': /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/stacktrace:164: undefined reference to `__glibcxx_backtrace_create_state' /opt/compiler-explorer/gcc-13.2.0/bin/../lib/gcc/x86_64-linux-gnu/13.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/ccXWeU5s.o: in function `std::stacktrace_entry::_M_get_info(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, int*) const': /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/stacktrace:196: undefined reference to `__glibcxx_backtrace_pcinfo' /opt/compiler-explorer/gcc-13.2.0/bin/../lib/gcc/x86_64-linux-gnu/13.2.0/../../../../x86_64-linux-gnu/bin/ld: /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/stacktrace:206: undefined reference to `__glibcxx_backtrace_syminfo' /opt/compiler-explorer/gcc-13.2.0/bin/../lib/gcc/x86_64-linux-gnu/13.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/ccXWeU5s.o: in function `std::basic_stacktrace<std::allocator<std::stacktrace_entry>>::current(std::allocator<std::stacktrace_entry> const&)': /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/stacktrace:259: undefined reference to `__glibcxx_backtrace_simple'
解决方法
GCC编译链接的修复
GCC编译通过但链接失败,是因为std::stacktrace依赖libstdc的回溯库,需要在链接时显式指定链接libstdc++_backtrace库,同时开启C23支持:
g++ -std=c++23 your_file.cpp -lstdc++_backtrace
Clang编译的修复
Clang出现的operator delete匹配错误,根源是Clang不支持GCC的__builtin_operator_delete带大小参数的重载形式,可通过以下两种方式解决:
- 切换到GCC编译:采用上述GCC的编译命令即可正常运行。
- 编译时定义宏:添加
-D_GLIBCXX_USE_NO_DELETE_WITH_SIZE宏禁用带大小的delete操作,同时链接libstdc++_backtrace库:clang++ -std=c++23 -D_GLIBCXX_USE_NO_DELETE_WITH_SIZE your_file.cpp -lstdc++_backtrace
内容的提问来源于stack exchange,提问作者Fedor
相关产品推荐
相关产品推荐

