I have a class structure
Class A
{
Object of classB
Object of classC
}
Now for this purpose I have files A.h/A.m B.h/B.m , C.h/C.m
Now I have a separate file G.h which has a number of global variables which are used by all the class A,B,C
Now I have imported B.h , C.h , G.h inside A.h . I also have separately imported the file G.h inside B.h and C.h
Now when build the project , I receive a link error for duplicate symbol on the global varibles (which is probably due to multiple inclusion of the file G.h) .
How can I solve this ??? The following is my code structure
//A.h
#import "B.h"
#import "C.h"
#import "G.h"
@interface A : NSObject {
B *b;
C *c;
}
//B.h
#import "G.h"
//C.h
#import "G.h"
//G.h
A *a=nil;
@interface G : NSObject { //whole class is empty}
You should declare your globals in
G.hbut define them inG.m, so that they’re only linked once in the target. So if, for example, you have a string constant that’s globally defined, you would do:G.hG.mIn your pasted code, it’s the
Ainstance that’s being duplicated. It’s imported into each other translation unit, but with the same name every time which means that the compiled objects can’t be linked. Assuming you want a sharedAinstance calleda, you’d do:G.hG.m