Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

barryzuckercorn

macrumors newbie
Original poster
Dec 25, 2008
2
0
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.
 

barryzuckercorn

macrumors newbie
Original poster
Dec 25, 2008
2
0
Post all of your code, it's hard to say without it.
-Lee

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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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.

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.
 

Attachments

  • Xcode001.png
    Xcode001.png
    52 KB · Views: 79
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.