Boost::Stacktrace minimal example

main.cpp

main.cpp
#include <boost/stacktrace.hpp>
#include <iostream>

void foo();

void bar() {
    // Capture and print the current stacktrace
    std::cout << "Stacktrace:\n" << boost::stacktrace::stacktrace() << std::endl;
}

void foo() {
    bar();
}

int main() {
    foo();
    return 0;
}

How to compile

build.sh
g++ -g main.cpp -std=c++17 -rdynamic -lboost_stacktrace_basic -o app

Example output

output.txt
$ ./app
Stacktrace:
 0# bar() in ./app
 1# foo() in ./app
 2# main in ./app
 3# 0x000072C53AE2A1CA in /lib/x86_64-linux-gnu/libc.so.6
 4# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 5# _start in ./app

Check out similar posts by category: Boost, C/C++