重复符号错误 - 全局常量 [英] 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仅在此类中使用,但我在另一个类实现中引用gotFilePathNotificaion。为了做到这一点,我导入这个头。
gotResultNotification is used only in this class (yet), but I reference gotFilePathNotificaion in another class implementation. To do it, I import this header.
当我尝试编译,我得到一个重复符号链接器错误关于gotFilePathNotification在这个标题。为什么会发生?
When I try to compile, I get a duplicate symbol linker error about gotFilePathNotification in this header. Why does it happen?
推荐答案
在文件的两个不同的编译单元中有两个具有相同名称的标识符范围。这违反了一种定义规则。您需要 -
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 -
-
声明全局变量标记在头文件中具有外部链接。
Declare the global variables marking to have external linkage in a header file.
extern NSString * const gotFilePathNotification;
现在提供仅一个源文件中的定义。 / p>
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.
这篇关于重复符号错误 - 全局常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!