使用 std::stacktrace 的编译和链接错误

问题描述 投票:0回答:1

我正在 Fedora 39 Linux 中工作,并且想开始使用 C++23 中的

std::stacktrace
,它可以在
libstdc++
中使用。

不幸的是,即使是最简单的例子,我也遇到了一些错误:

#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'

幸运的是,这两个错误都可以在 godbolt 上重现:https://gcc.godbolt.org/z/aovffv81s

如何修复这些错误?

c++ stack-trace libstdc++ c++23
1个回答
0
投票

您需要在 Clang 上使用

-fsized-deallocation

© www.soinside.com 2019 - 2024. All rights reserved.