0

How to use extern keyword to define global ivars, does anybody know this question?Thanks in advance.

Update from comments:

OK, I searched from the Internet that I should declare the variable in .h file,like below shows,

int name;
@interface testGlobal : NSObject
@end

And when I want to use it in one .m file, I should write like below,

extern int name;
@implementation ViewController

When I use it in the implementation file,I got the following error

duplicate symbol _name in:
/Users/jay_Gao/Library/Developer/Xcode/DerivedData/Audition-bahpwhcmpnzxppajcliyuhtuaywz/Build/Intermediates/Audition.build/Debug-iphonesimulator/Audition.build/Objects-normal/x86_64/Test.o
/Users/jay_Gao/Library/Developer/Xcode/DerivedData/Audition-bahpwhcmpnzxppajcliyuhtuaywz/Build/Intermediates/Audition.build/Debug-iphonesimulator/Audition.build/Objects-normal/x86_64/testGlobal.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)'
, can you explain this please? – I build successfully by remove the #import "testGlobal.h" from test.m. But what if I want to import that file testGlobal.h in other files, can not I do it? Should I only declare the global variable in AppDelegate.h? Since on other file will import AppDelegate.h?

I test it from my side and I can build it successfully, but I feel confuse that why should the global variable be declared in that way. The variable is outside any code block.

Jay
  • 113
  • 1
  • 11
  • First learn what an ivar is. – zaph Aug 29 '15 at 14:34
  • ivar is a variable, it is different from property – Jay Aug 29 '15 at 14:47
  • 1
    No. ivar is an abbreviation for ["instance variable"](https://en.wikipedia.org/wiki/Instance_variable). Note "instance", this implies instance of something, global implies not an instance of anything. BTW, these days you need to specify a language, the tags are a good place to do that. – zaph Aug 29 '15 at 14:51
  • You have `name` declared somewhere else. The full error message is always better than just part of it, update the question with it. – zaph Aug 29 '15 at 15:14
  • If you define a variable in a header file (.h) file it ends up being declared in every implementation file (.m) that imports the header file. Essentially the header file is copied into the files replacing #include/#import. That is why there are duplicate error messages. As @psobko demonstrates, extern in the header declare in **one** implementation file. – zaph Aug 29 '15 at 15:34
  • The reason why globals work in this way (or why there is even something called header files) is purely historical. We are talking about an era when compilers could compile one file at a time and only sequentially. It's a bit hard to explain without prior knowledge about compilers and other languages. The fact is, globals shouldn't be used at all. There are always better ways to implement something than using a global variable. – Sulthan Aug 29 '15 at 15:59

1 Answers1

0

You need to extern it in the interface and declare in the implementation of your testGlobal class:

//Global.h
extern int xyz;

@interface Global : NSObject
@end

//Global.m
#import "Global.h"

int xyz = 123;
@implementation Global
@end

//ViewController.m
#import "ViewController.h"
#import "Global.h"

@implementation ViewController

- (void)viewDidLoad {
    NSLog(@"%d", xyz);
    [super viewDidLoad];
}
psobko
  • 1,548
  • 1
  • 14
  • 24