5.7. Debugging Optimized Code

You can use multiple levels of optimization while building the output binaries. The generated executable code may be different for each level of optimization. If you step through the optimized code in gdb, the gdb may not step as you expected in some cases. Let us compile the sumopt.c program and see how optimization does affect. Listing below is the program source code:

#include <stdio.h>
main ()
{
  int num1, num2, total ;

  num1 = 4;
  num2 = 6;
  total = num1 + num2;

  printf("\nThe sum is : %d\n", total);
}

Now let us compile the code without optimization using the following command and then step through it.

gcc -g sumopt.c -o sumopt

Following is the gdb session to step through the code. As you can see, it is quite ...

Get Linux Development Platform: Configuring, Using, and Maintaining a Complete Programming Environment, The 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.