Solve the problem of invalid breakpoints and misplaced breakpoints when debugging C++ code in VScode

problem background

Recently, I was studying Dr. Gao Xiang's classic course "Visual SLAM Fourteen Lectures (Second Edition)". I used the C++ code in the supporting Github to study. When debugging, I found that the breakpoint was invalid and misplaced. I checked some information and succeeded. Solved, record it.
Editor : VSCode
Operating system : Ubuntu20.04
Debugging method : cmake + make
For a tutorial on debugging C++ code in VScode, you can refer to this: Use VS Code + CMake to debug c++ programs under Linux

Problem Description

At the beginning, CMakeLists.txt was set to release mode (Release). After setting the breakpoint, the breakpoint was invalid during debugging, that is, it could not stop at any breakpoint. After setting in CMakeLists.txt, set(CMAKE_BUILD_TYPE "Debug")debugging It can stop, but it does not stop at the breakpoint, but in other places, as shown in the figure below:
breakpoint misplacement
From this picture, we can see that the breakpoint is set at line 42, but it stops at line 46 during debugging. At the entrance of the for loop, it is obviously not what we want.
What's even more outrageous is that in some positions, the breakpoint is set on a certain line before debugging, but it appears in another place during debugging. As shown in the figure below, the breakpoint position set before debugging starts: Breakpoint position during
before debugging
debugging : while debugging
It can be seen that the breakpoint is at line 92 before debugging, but it is at line 95 after debugging. Later, after consulting the data, it is found that this is caused by code compilation optimization .

Solution

When using cmake + make to debug C++ code in VScode, if you want to perform breakpoint debugging normally, you need to pay attention to the following two points in the CMakeLists.txt file:

  1. Set to "Debug" mode : set(CMAKE_BUILD_TYPE "Debug"), if set to "Release" mode, after setting the breakpoint, it will be invalid by default during debugging. After entering debugging, the breakpoint will become a hollow breakpoint, and the program will not stop at any breakpoint Down.
  2. Cancel code compilation optimization : must be commented out set(CMAKE_CXX_FLAGS "-O3")(or not written), otherwise "Any level of optimization will bring about changes in the code structure, which will change the execution sequence of the target code beyond recognition, resulting in a serious lack of debugging information."

In summary, the settings in CMakeLists.txt are as follows:

set(CMAKE_BUILD_TYPE "Debug")
# set(CMAKE_CXX_FLAGS "-O3")

Able to stop gracefully at breakpoints while debugging:
insert image description here
problem resolved successfully.

reference article

  1. Use VS Code + CMake to debug c++ programs under Linux
  2. CMake command parsing set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS}
  3. Add a link to describe the gcc -O0 -O1 -O2 -O3 four-level optimization options and what optimization each level does

Guess you like

Origin blog.csdn.net/hypc9709/article/details/125906413