big
big

Reputation: 1938

Duplicate Symbol error xcode iphone

I am getting duplicate error in my xcode while build and run

I have two files file1.m and file2.m both use 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];

When Build and run it gives me error duplicate symbol "count" in file1.o and file2. o if I change their names to count1 and count2 I will not get any error.

In both file1.m and file2.m I am trying to make global variables.

Is there any way by which I can use same names of variable and functions in both the files

Upvotes: 3

Views: 804

Answers (1)

Carl Norum
Carl Norum

Reputation: 225162

Make them static:

static int count = 0;
static int arr[2][2];

Note that they'll refer to different variables. If you want to them to refer to the same variables, leave it the way you have it in one file and declare them extern in the other file:

extern int count;
extern int arr[2][2];

It's common to put those extern declarations in a common header.

Upvotes: 5

Related Questions