Setting breakpoints and inspection variable values

In the following example, we will learn how to set breakpoints, continue, step into or step over a function, print values of variables, and how to use help in gdb. The source code is as follows:

//ch13_gdb_1.cpp#include <iostream>float multiple(float x, float y);int main(){ float x = 10, y = 20; float z = multiple(x, y); printf("x=%f, y=%f, x*y = %f\n", x, y, z); return 0;}float multiple(float x, float y){ float ret = x + y; //bug, should be: ret = x * y; return ret;}

As we mentioned in Chapter 3, Details of Object-Oriented Programming, let's build this program in debug mode, as follows:

~wus1/chapter-13$ g++ -g ch13_gdb_1.cpp -o ch13_gdb_1.out

Note that for g++, the -g option means the debugging ...

Get Expert C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.