Duplicate iphone xcode symbol error
I am getting duplicate error in my xcode when building and running
I have two files file1.m and file2.m are using the same variable and function names
file1.h
#import <UIKit/UIKit.h>
@interface file1 : UIViewController {
IBOutlet UILabel *result;
}
-(IBAction)home;
@end
file1.m
#include<file1.h>
@implementation file1
int count = 0;
int arr[2][2];
file2.h
#import <UIKit/UIKit.h>
@interface file2 : UIViewController {
IBOutlet UILabel *result;
}
-(IBAction)home;
@end
file2.m
#include<file2.h>
@implementation file2
int count = 0;
int arr[2][2];
On build and run, it gives me a duplicate "count" character in file1.o and file2. o if i change my names to count1 and count2 i don't get any errors.
In file1.m and file2.m, I am trying to make global variables.
Is there a way that I can use the same variable and function names in both files
source to share
Make them static
:
static int count = 0;
static int arr[2][2];
Please note that they will refer to different variables. If you want them to refer to the same variables, leave it as you have in one file and declare them extern
in another file:
extern int count;
extern int arr[2][2];
It's common to put those ads extern
in a general headline.
source to share