Extreme newbie question re: external variables/prepended underscores

Discussion in 'Mac Programming' started by barryzuckercorn, Dec 25, 2008.

  1. barryzuckercorn macrumors newbie

    Joined:
    Dec 25, 2008
    #1
    Hi all. As I said, I'm pretty much a total newcomer to programming of any type. I'm going through Kochan's textbook on Objective-C, and I've come to the section on variable scope. In that section, there's an example in which a global integer variable called "gGlobalVar" is declared in "main.m". Then, in one of the classes that's imported into "main.m", there's a method (setgGlobalVar) that declares "gGlobalVar" as an external variable and sets its value to 100.

    I've gone ahead and plugged Kochan's example into Xcode, but when I go to compile it, I end up getting an error message about an undefined symbol called "_gGlobalVar". I know that somewhere along the way, an underscore gets slapped on the front of the variable name, but I don't know how to account for this and deal with it so that the program actually compiles and runs without any problems.

    Any advice to get me over this would be awesome. Thanks.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Post all of your code, it's hard to say without it.
    -Lee
     
  3. barryzuckercorn thread starter macrumors newbie

    Joined:
    Dec 25, 2008
    #3
    OK, here goes...

    Interface (Foo.h)
    #import <objc/Object.h>

    @interface Foo: Object
    -(void) setgGlobalVar: (int) val;
    @end

    Implementation (Foo.m)
    #import "Foo.h"

    @implementation Foo
    -(void) setgGlobalVar: (int) val
    {
    extern int gGlobalVar;
    gGlobalVar = val;
    }
    @end

    Main (main.m)
    #import "Foo.h"
    #import <stdio.h>

    int main (int argc, char *argv[])
    {
    int gGlobalVar;
    Foo *myFoo = [[Foo alloc] init];
    printf ("%i ", gGlobalVar);

    [myFoo setgGlobalVar: 100];

    printf ("%i\n", gGlobalVar);
    [myFoo free];
    return 0;
    }

    When I compile, I get "Undefined symbols: _gGlobalVar"

    Thanks.

    -bz
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Move the declaration of gGlobalVar outside of the scope of main. It is a local variable with its scope limited to main right now.

    -Lee

    P.S. I know the book is teaching this for a reason, but hopefully notes that global variables should be avoided if possible.
     
  5. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    You have not typed the example in correctly, and that's why you are getting the link error. (not a compile error, and not a runtime error, but a link error).

    The example shows this:
    Code:
    #import "Foo.h"
    #import <stdio.h>
    
    [color=red]int gGlobalVar = 5 ;[/color]
    
    int main (int argc, char *argv[])
    {
    Foo *myFoo = [[Foo alloc] init];
    printf ("%i ", gGlobalVar);
    
    [myFoo setgGlobalVar: 100];
    
    printf ("%i\n", gGlobalVar);
    [myFoo free];
    return 0;
    }
    which is the same thing that Lee mentioned you do to fix it.
     

    Attached Files:

Share This Page