Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ld: duplicate symbol - caused by const

Tags:

I was defining a NSString to use as error domain in NSError and was copying how ASIHttpRequest was doing there's.

NSString* const FPServerAPIErrorDomain = @"FPServerAPIErrorDomain";

I put the const in its own .h file // FPServerAPICoordinatorConstants.h

#ifndef FirePlayer_FPServerAPICoordinatorConstants_h
#define FirePlayer_FPServerAPICoordinatorConstants_h

NSString* const FPServerAPIErrorDomain = @"FPServerAPIErrorDomain";

#endif

but when I included it in more than one .m

SomeFile.m

#import "FPServerAPICoordinatorConstants.h"

SomeOtherFile.m

#import "FPServerAPICoordinatorConstants.h"

I got linker error 'duplicate symbol'

ld: duplicate symbol _FPServerAPIErrorDomain in SomeFile.o and ....SomeOtherFile.o for architecture armv7

so I change the const to #define and it worked ok.

//  FPServerAPICoordinatorConstants.h

#ifndef FirePlayer_FPServerAPICoordinatorConstants_h
#define FirePlayer_FPServerAPICoordinatorConstants_h


//THIS WAS TRIGGERING link errors
//NSString* const FPServerAPIErrorDomain = @"FPServerAPIErrorDomain";
//working OK
#define FPServerAPIErrorDomain @"FPServerAPIErrorDomain"

#endif

But is there a way to get the const in global space not to throw 'duplicate symbol'?

like image 992
brian.clear Avatar asked Mar 06 '12 12:03

brian.clear


1 Answers

In your header file you want:

extern NSString *const FPServerAPIErrorDomain;

and then in an implementation file (so probably you want a FPServerAPICoordinatorConstants.m) you will want:

NSString *const FPServerAPIErrorDomain = @"FPServerAPIErrorDomain";

Then you can import the header into multiple file and not get duplicate symbol errors.

[By the way, you don't need the #ifndef guards if you're using #import.]

like image 64
mattjgalloway Avatar answered Sep 18 '22 15:09

mattjgalloway