重复符号错误——全局常量 [英] Duplicate symbol error — global constant

查看:46
本文介绍了重复符号错误——全局常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类的头部,接口声明之外,我已经声明了全局常量:

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 -

  1. 在头文件中声明全局变量标记为具有外部链接.

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

extern NSString * const gotFilePathNotification;

  • 现在在一个源文件中提供定义.

  • 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆