TheLartians / ModernCppStarter

🚀 Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.
https://thelartians.github.io/ModernCppStarter
The Unlicense
4.33k stars 381 forks source link

how to print the stack to trace an error? #190

Closed Guangyi-Zhang closed 2 months ago

Guangyi-Zhang commented 3 months ago

Say I write the following simple function at include/greeter/greeter.h, and then I call it in the main function in standalone/source/main.cpp.

#include <map>

void testfunc() {
  std::map<int,int> m;
  m.at(1);
}

Apparently, there will be no-key error. However, running the standalone doesn't reveal any clue about where the error is. It only says:

$ ./build/standalone/DST
libc++abi: terminating due to uncaught exception of type std::out_of_range: map::at:  key not found
[1]    81303 abort      ./build/standalone/Greeter

This is quite a pain in development, as the user has no clue about the location of the mistake. Is there a way to print the call stack or the line number where the error originally happens?

Guangyi-Zhang commented 2 months ago

I asked a silly question, and it could be done easily with gdb.