xcode #import 仍然出现“重复符号”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2754252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:19:23  来源:igfitidea点击:

#import still gets "duplicate symbol" error

iphonecxcodeimportcompiler-errors

提问by Susanna

When I compile my iPhone app, xCode gives "duplicate symbol" error for my variables in MyConstants.h

当我编译我的 iPhone 应用程序时,xCode 在 MyConstants.h 中为我的变量提供“重复符号”错误

I thought if I used:

我想如果我使用:

#import "MyConstants.h"

it would avoid that?

它会避免吗?

But I still have the problem.

但我仍然有问题。

Added info:

添加信息:

  • The error occurs during "linking". (I'm just using xCode's "Build and Go" button.)

  • 在“链接”期间发生错误。(我只是使用 xCode 的“Build and Go”按钮。)

  • I also tried the (unnecessary with #import) #ifndef/def method, too.

    Maybe I should just ask this:

    If you needed to access a constant in EVERYpart of ALLyour source code files... what would you put in your .h file? What would you use to include that constant in other parts of your code.

    I thought (but I guess it's not) it was simple as:

  • 我也尝试了(不需要#import)#ifndef/def 方法。

    也许我应该问这个:

    如果您需要访问所有源代码文件的每个部分中的常量……您会在 .h 文件中放什么?您将使用什么将该常量包含在代码的其他部分中。

    我认为(但我想不是)它很简单:

  • MyConstants.h> int thisIsGlobal = 123;

    (No where am I re-defining thisIsGlobal anywhere in any code.)

    And then just "#import MyConstants.h" at the top of each of my other source files.

  • MyConstants.h> int thisIsGlobal = 123;

    (没有我在任何代码中的任何地方重新定义 thisIsGlobal 的地方。)

    然后在我的每个其他源文件的顶部只是“#import MyConstants.h”。

  • 回答by dreamlax

    What you can do is put in your header (MyConstants.h):

    你可以做的是把你的标题(MyConstants.h):

    extern const int MyConstant;
    extern NSString * const MyStringConstant;
    

    And in a source file, include the header above but define the constants (MyConstants.m):

    在源文件中,包含上面的标题但定义常量 ( MyConstants.m):

    const int MyConstant = 123;
    NSString * const MyStringConstant = @"SomeString";
    

    Then, you simply need to include the header in any other source file that uses either of these constants. The header is simply declaring that these constants exist somewhere, so the compiler won't complain, because it's the linker's job to resolve these constant names. The source file that contains your constant definitions gets compiled, and the linker sees that this is where the constants are, and resolves all of the references found in the other source files.

    然后,您只需将标头包含在使用这些常量之一的任何其他源文件中。标头只是声明这些常量存在于某处,因此编译器不会抱怨,因为解析这些常量名称是链接器的工作。包含常量定义的源文件被编译,链接器看到这是常量所在的位置,并解析在其他源文件中找到的所有引用。

    The problem with declaring and defining a constant in a header (that is not declared as static) is that the compiler treats it as an independent global for each file that includes that header. When the linker tries to link all of your compiled sources together it encounters the global name as many times as you have included MyConstants.h.

    在头文件(未声明为static)中声明和定义常量的问题在于,编译器将其视为包含该头文件的每个文件的独立全局变量。当链接器尝试将所有编译源链接在一起时,它遇到全局名称的次数与您包含的MyConstants.h.

    回答by Stephen Canon

    Two options:

    两种选择:

    static const int thisIsGlobal = 123;
    

    or

    或者

    #define thisIsGlobal 123
    

    回答by Everton Cunha

    I use like this, and works: (in a .h outside @interface)

    我是这样使用的,并且有效:(在@interface 之外的 .h 中)

    static NSString * const mkLocaleIdentifierUS = @"en_US";
    static NSString * const mkLocaleUserSystemSettings = nil;
    

    回答by justin

    This is because the symbol name in question (thisIsGlobal) is being emitted into every object file created, where the header containing the declaration for thisIsGlobal is included and visible.

    这是因为有问题的符号名称 (thisIsGlobal) 被发送到每个创建的目标文件中,其中包含 thisIsGlobal 声明的标头被包含并且可见。

    The examples provided by another poster: 'extern const int MyConstant;' is the best way, unless you need the value to be visible, in which case you can use an enum:

    另一张海报提供的示例:'extern const int MyConstant;' 是最好的方法,除非您需要该值可见,在这种情况下您可以使用枚举:

    int thisIsGlobal = 123; // bad

    enum { thisIsGlobal = 123 }; // ok

    int thisIsGlobal = 123; // 坏的

    枚举 { thisIsGlobal = 123 }; // 好的

    using static will emit a lot of hidden symbols in a large program -- don't use it. Using a define is scary as well (considering there are safer alternatives available, why not use them?).

    使用 static 会在大型程序中发出大量隐藏符号——不要使用它。使用定义也很可怕(考虑到有更安全的替代方案,为什么不使用它们?)。

    回答by Alex Reynolds

    I usually put my application constants file in the Xcode project's MyApplication_Prefix.pchfile, usually located within the Other Sourcesgroup. Any header file included in this pchfile will be included from all files in your project.

    我通常将我的应用程序常量文件放在 Xcode 项目的MyApplication_Prefix.pch文件中,通常位于Other Sources组内。此文件中包含的任何头文件pch都将从项目中的所有文件中包含。

    After adding this include statement, you would then no longer need to include your MyConstants.hfile from every file in your project — it will be included automatically.

    添加此 include 语句后,您将不再需要MyConstants.h从项目中的每个文件中包含您的文件——它将自动包含在内。