Thread: GDB Not Showing Variable value

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    GDB Not Showing Variable value

    Hi All

    I wrote a small sample program for testing GDB in linux.

    The code is:

    Code:
    #include<stdio.h>
    
    void fun()
    {
     int x=20;
    
     printf("\n Inside Fun");
    }
    int main()
    {
    
     int x=10; 
     
     printf("\n In main \n");
     fun();  // Put Break point here
    
    }
    I put break point at fun() and try to print out value of x using:
    print x
    But it give me error that:
    No symbol "x" in current context.
    Can any body help me in this. I am using Ubuntu.

    Thanks

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    After setting the breakpoint, you have to start the program running so it can run up to that point. The command is "run args" where args is optional and represents the args you'd pass on the command line, if any.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Soory that I didn't mention that it is coming after running the program.
    It print successfully print:
    In main
    But after wards not the value of x.

    Thanks

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Did you compile it with the -g option (to include debugging information) ?
    gcc -Wall -Wextra -g yourprog.c
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    103
    Oh Thanks.. It's working now.

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Also, what can sometimes happen, is that the compiler notices that x is not used and just optimises it away. This is quite common in debugging gcc programs, especially if you compiled with the -O switches and have code like that.

    When debugging in a debugger, include debug statements (-g) and turn off all optimisations (-O0).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help showing in array
    By tdiddy in forum C Programming
    Replies: 1
    Last Post: 04-07-2011, 07:48 PM
  2. c++ not showing a variable
    By Mythic Fr0st in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2007, 11:05 PM
  3. Two forms showing at once
    By Tperry23 in forum C# Programming
    Replies: 0
    Last Post: 06-07-2005, 11:51 PM
  4. Bill showing off...
    By deathstryke in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 12-11-2002, 01:31 PM
  5. Pic not showing up
    By Hunter2 in forum Game Programming
    Replies: 14
    Last Post: 11-13-2002, 01:19 PM