Multiple Interfaces having same implementation - Duplicate Symbol Error- Objective - c

StackOverflow https://stackoverflow.com/questions/19472325

سؤال

I was working on a static library with an NSObject class named 'ClientRequest'

I had to import the NSObject of type ClientRequest to my other classes: A, B and C.

So all the above mentioned classes implements like below

#import "ClientRequest.h"

@interface A : ClientRequest

#import "ClientRequest.h"

@interface B : ClientRequest

#import "ClientRequest.h"

@interface C : ClientRequest    

The static library project(.xcodeproj) was added to the my own project and the target included with the static libary. When I ran my own project, I had the 'duplicate symbol _API_BASE_ for i386 architecture' error for the A and B, A and C & A and ClientRequest.

Any Idea how to tackle this problem? The Classes A, B and C uses the methods of ClientRequest.

هل كانت مفيدة؟

المحلول

Exactly what you are doing is hard to determine without additional information. However from what is given it appears that you are defining API_BASE in the ClientRequest.h file so that in every file you include a new item is defined and you end up with multiple items.

One way this can occur is if you define a global variable incorrectly.

If you declare a global variable in ClientRequest.h as, say:

int API_BASE;

then every file compiled which includes the header will define its own copy of that variable and you'll get duplicate symbol errors. The correct way to do it is to declare the variable as `extern in the header:

extern int API_BASE;

and to define it once in ClientRequest.m:

int API_BASE;

The extern keyword indicates that you are declaring the name and type of a variable which will be defined elsewhere.

All function/methods declaration are implicitly external, but the keyword is required for variables.

HTH

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top