c++stack-tracelibstdc++c++23

Compilation and linking errors using std::stacktrace


I am working in Fedora 39 Linux, and would like to start using std::stacktrace from C++23, which is available in libstdc++.

Unfortunately, I bumped in some errors even with the simplest example:

#include <stacktrace>

int main() {
    (void)to_string( std::stacktrace::current() );
}

In Clang, I get a long compilation error

/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
      |      ^                      ~~~~~~~~~~~~~~~~~~~~~
...

And in GCC, the compilation goes fine, but I get linking errors:

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

Fortunately, both errors can be reproduced on godbolt: https://gcc.godbolt.org/z/aovffv81s

How one can fix these errors?


Solution

  • You need -fsized-deallocation on Clang (plus -lstdc++_libbacktrace, of course, and maybe -no-pie).