I have a class with a printDebug method. Its not used anywhere in the code but I would like to use it when I am debugging with gdb (using call). This is basically to print the contents of the object in a nicely formatted way, for instance I may have a vector of sets. What is the g++ option to use for this? I have tried -O0 but that does not work.
The work around I used was to make a psuedo call in the constructor to debugPrint and provide a bool indicating if you actually want to print or do nothing. This works fine but there has to be a better way to do this.
If I understand correctly -O0 should not do any optimisations so dead code should not be eliminated but perhaps I am wrong.
If you have a method that is not used anywhere on the code gcc smart features can identify this and ignore it while compiling your application. That’s why when you display the symbols (using nm) of the application that method doesn’t show on the results.
However, if you want to force that method to be compiled anyway you need to specify the _attribute_
usedon the method declaration. For instance:For testing purposes I compiled this source code with
-g:What I’m about to say is probably unnecessary, but I’ll do it anyway: notice that
my_objis declared as a local variable inside main(). This means I only have access to the methodpublicPrint()while I’m debugging code inside this scope. When the code execution jumps to the beginning of getchar(), code execution will be at another scope, i.e. another stack frame, andmy_objwill no longer exist in this new context. This is just a heads up.On gdb, if you set a breakpoint where
my_objis valid, you can execute the methodpublicPrint()through:call my_obj.publicPrint()