【问题标题】:Duplicate symbol error in objective-c目标c中的重复符号错误
【发布时间】:2012-07-12 13:57:46
【问题描述】:

我的 Objective-C 应用程序中需要一些全局变量。为此,我创建了 Globals 类(继承 NSObject)并将只读属性放入其中。我还声明了一些常量,如下所示:

imports, etc.
.
.
.
#ifndef GLOBALS_H
#define GLOBALS_H

const int DIFFICULTY_CUSTOM = -1;
const int other_constants ...
#endif
.
.
.
interface, etc.

但是当我尝试编译它时,我得到链接器错误:“重复符号 DIFFICULTY_CUSTOM”。为什么会发生这种情况,ifndef 应该预防吗?

【问题讨论】:

标签: objective-c compiler-construction linker


【解决方案1】:

问题是const int DIFFICULTY_CUSTOM = -1; 在您为其包含标头的每个目标文件中分配了该名称的 int。
您应该只在每个标题中声明 extern const int DIFFICULTY_CUSTOM;。然后应在一个目标文件(即 .m 或 .c )中定义实际值 const int DIFFICULTY_CUSTOM = -1;

在这种情况下,我将只使用#define 来设置值。

【讨论】:

    【解决方案2】:

    我会这样做:

    constants.m:

    const int DIFFICULTY_CUSTOM = -1;
    

    constants.h:

    extern const int DIFFICULTY_CUSTOM;
    

    .pch 文件中:

    #import "constants.h"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      相关资源
      最近更新 更多