Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate symbol error — global constant

In the header of the class, outside of interface declaration, I've declared global constants:

NSString * const gotFilePathNotification = @"gotFilePath";
NSString * const gotResultNotification = @"gotResultOfType";

gotResultNotification is used only in this class (yet), but I reference gotFilePathNotificaion in another class implementation. To do it, I import this header.

When I try to compile, I get a duplicate symbol linker error about gotFilePathNotification in this header. Why does it happen?

like image 688
Max Yankov Avatar asked Jun 08 '12 23:06

Max Yankov


1 Answers

You have two identifier(s) with same name across two different compilation unit(s) at file scope. This violates One Definition Rule. Instead you need to -

  1. Declare the global variables marking to have external linkage in a header file.

    extern NSString * const gotFilePathNotification;
    
  2. Now provide the definition in only one source file.

    NSString * const gotFilePathNotification = @"gotFilePath";
    

Now where ever you need to use these variables, include the header in the source file.

like image 156
Mahesh Avatar answered Oct 18 '22 21:10

Mahesh