C++ debugging method to bypass library files
Sometimes we want to only debug our own code when debugging c++ code, without entering the library file. For example, in the following piece of code, we want to execute at f()
will step into
directly enter the body of display()
the function , but unfortunately, until 2022, c++ does not support the "Just my code" debugging option very well.
#include "stdafx.h"
#include <functional>
#include <iostream>
void display() {
std::cout << "Hello" << std::endl; // step into should go here
}
int main() {
std::function<void()> f = &display;
f(); // Step into here should go directly to "display()"
}
Currently, I have found the following solutions.
Solution 1: Debugging with Visual Studio
When using visual studio to debug c++ code, the "just my code" option is enabled by default, so if you are generating a console program for the Windows platform, executing step into will skip the library file by default.
But this method does not support local computer Cmake projects.
Solution 2: Visual Studio + Cmake
Fortunately, there is an option in Cmake to enable "Just My Code" support for the Visual Studio debugger. Just add the following line to the CmakeLists.txt file.
set (CMAKE_VS_JUST_MY_CODE_DEBUGGING 1)
However, this method only supports Cmake projects under the local computer. If you want to build a Cmake project under remote Linux, it is still not supported.
Others, there is a solution to [^1] from the level of gdb, but the effect is still not ideal.
References
[^1] Skipping standard C++ library during debug session in gdb (reversed.top)
[^2] Ability to debug only my code (aka Just my code) : CPP-14618 (jetbrains.com)