Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:41:27+00:00 2026-05-23T09:41:27+00:00

I have a class with a printDebug method. Its not used anywhere in the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:41:28+00:00Added an answer on May 23, 2026 at 9:41 am

    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_ used on the method declaration. For instance:

      1 
      2 #include <iostream>
      3 #include <stdio.h>
      4 
      5 
      6 class aClass
      7 {
      8     public:
      9         void __attribute__ ((used)) publicPrint()
     10         {
     11             std::cout << "public method" << std::endl;
     12         }
     13 };
     14 
     15 
     16 int main()
     17 {
     18     aClass my_obj;
     19 
     20     getchar();
     21 }
    

    For testing purposes I compiled this source code with -g:

    g++ -g print_dbg.cpp -o print_dbg
    

    What I’m about to say is probably unnecessary, but I’ll do it anyway: notice that my_obj is declared as a local variable inside main(). This means I only have access to the method publicPrint() 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, and my_obj will no longer exist in this new context. This is just a heads up.

    On gdb, if you set a breakpoint where my_obj is valid, you can execute the method publicPrint() through: call my_obj.publicPrint()

    $ gdb print_dbg 
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i486-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/karl/workspace/gdb/print_dbg...done.
    
    (gdb) list main
    12              }
    13      };
    14
    15
    16      int main()
    17      {
    18          aClass my_obj;
    19
    20          getchar();
    21      }
    (gdb) break main
    Breakpoint 1 at 0x804871d: file print_dbg.cpp, line 20.
    
    (gdb) run
    Starting program: /home/karl/workspace/gdb/print_dbg 
    Breakpoint 1, main () at print_dbg.cpp:20
    20          getchar();
    
    (gdb) call my_obj.publicPrint()
    public method
    
    (gdb)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class in server side and I want use method of this class
I have class dot and have html code like that: <span class=dot>Text1</span> <span class=dot>Text2</span>
I have class where the relevant part looks like class C { void Method<T>(SomeClass<T>
I have class method that returns a list of employees that I can iterate
I have class like public class ProgressBars { public ProgressBars() { } private Int32
I have class X and in it there is a static method called doStuff()
I have class that I believe should not be a singleton or static class.
I have class with a forwarding method foo : void foo( Concrete c, String
I have class A with its inner class defined A1 and class B with
I have class which has a method that needs to return three DataTables. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.