Use the gdb command line for single-step debugging in linux, and the whole process is described as follows:
1. Create a new folder main in the current path, enter the folder, and create a new file main.cpp
mkdir main
cd hand
touch main.cpp
2.compile, add -g, after executing one more slove file
g++ -g main.cpp -o slove
main.cpp is the bisection method to solve the equation roots
#include <cstdio> #include <cmath> #include<iostream> double bisection(int p, int q, double(*func)(int, int, double)); double f(int p, int q, double x); int main() { int p; int q; //scanf_s("%d %d", &p, &q); //printf_s("%.4lf\n", bisection(p, q, f)); cin >> p >> q; cout << bisection(p, q, f) << endl; return 0; }
3. Enter gdb through the command line to execute solve, which means to debug the solve file
gdb ./solve
Indicates that it has entered the debugging state
4. Add a breakpoint, single step, continue. Add a breakpoint at the main function
b (break): add a breakpoint
r(run): run the program from scratch
n (next): next step
c (continue) : the program continues to run until the next breakpoint is encountered or the program ends
q (quit ): exit the program interface
Execute the following command
b main
A breakpoint has been inserted