#import 仍然得到“重复符号”错误
当我编译我的 iPhone 应用程序时,xCode 为 MyConstants.h 中的变量给出“重复符号”错误,
我想如果我使用:
#import "MyConstants.h"
它会避免这种情况吗?
但我仍然有问题。
添加信息:
也许我应该问这个:
如果您需要访问源代码文件的所有部分中的常量...您会在 .h 文件中放入什么内容?您将使用什么将该常量包含在代码的其他部分中。
我认为(但我想事实并非如此)它很简单:
(我在任何代码中的任何位置都没有重新定义 thisIsGlobal。)
然后只需在我的其他每个源文件的顶部添加“#import MyConstants.h”即可。
When I compile my iPhone app, xCode gives "duplicate symbol" error for my variables in MyConstants.h
I thought if I used:
#import "MyConstants.h"
it would avoid that?
But I still have the problem.
Added info:
(I'm just using xCode's "Build and Go" button.)
Maybe I should just ask this:
If you needed to access a constant in EVERY part of ALL your 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:
(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以做的就是放入标头 (
MyConstants.h
):并在源文件中包含上面的标头,但定义常量 (
MyConstants.m
):然后,您只需将标头包含在使用这些常量之一的任何其他源文件中。标头只是声明这些常量存在于某处,因此编译器不会抱怨,因为解析这些常量名称是链接器的工作。包含常量定义的源文件被编译,链接器发现这是常量所在的位置,并解析在其他源文件中找到的所有引用。
在标头中声明和定义常量(未声明为静态)的问题在于,编译器将其视为包含该标头的每个文件的独立全局变量。当链接器尝试将所有编译源链接在一起时,它遇到全局名称的次数与您包含
MyConstants.h
的次数一样多。What you can do is put in your header (
MyConstants.h
):And in a source file, include the header above but define the constants (
MyConstants.m
):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 includedMyConstants.h
.两个选项:
或
Two options:
or
我这样使用,并且有效:(在 @interface 之外的 .h 中)
I use like this, and works: (in a .h outside @interface)
这是因为相关符号名称 (thisIsGlobal) 被发送到创建的每个对象文件中,其中包含并可见包含 thisIsGlobal 声明的标头。
另一位发帖者提供的示例:“extern const int MyConstant;”是最好的方法,除非您需要该值可见,在这种情况下您可以使用枚举:
使用 static 会在大型程序中发出大量隐藏符号——不要使用它。使用定义也很可怕(考虑到有更安全的替代方案可用,为什么不使用它们呢?)。
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.
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:
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?).
我通常将应用程序常量文件放在 Xcode 项目的
MyApplication_Prefix.pch
文件中,该文件通常位于Other Sources
组中。此pch
文件中包含的任何头文件都将包含在项目中的所有文件中。添加此包含语句后,您将不再需要包含项目中每个文件中的
MyConstants.h
文件 - 它将自动包含。I usually put my application constants file in the Xcode project's
MyApplication_Prefix.pch
file, usually located within theOther Sources
group. Any header file included in thispch
file will be included from all files in your project.After adding this include statement, you would then no longer need to include your
MyConstants.h
file from every file in your project — it will be included automatically.