Why is Xcode compiling all of my code twice, resulting in linker errors for any globals?
When I look at my Build Results window, there are duplicate entries for each Compile .m, Precompile .pch, and Link .
Whenever I try to add a global, even as a static constant, I get a linker error.
In the linking stage, I can see that one run is for armv6 and the other is for armv7 (when compiling for an iOS Device).
There is no duplication done or linker errors when compiling for the iOS Simulator however.
Is this an issue (beyond the obvious issue of the linker errors)? If so, will it cause performance issues? How can开发者_如何学运维 I rectify this?
I'm pretty sure the double messages is caused by Universal App compiling.
The short answer is, don't use globals. :D Look up extern
use with objective-c, it may help you out with creating a global. If you are building for arm6 and arm7, you will have duplicate build entries as they are different assemblies.
Likely what happens when you create a static const
as a global is that it gets defined in every file. Put it in one .m file and add the extern
keyword in the others. That may be wrong, however, as I don't use globals. (and neither should you :D)
You shouldn't put definitions in a header file.
Declarations are things like int add(int a, int b);
and extern int c;
.
Definitions are things like int add(int a, int b) { return a+b; }
and int c;
.
If you define the global variable int c;
in a header file, every source file that #includes it will define a symbol called "c". The linker doesn't like it: There are 2 (or 3, or 4...) different things called c
but they all need to point to the same one. Which one should it use? (It's equivalent to defining two (non-static) functions with the same name, or two classes with the same name.)
Instead, stick extern int c;
in the header file and int c;
in one source file.
(The "duplicate" compiles for armv6 and armv7 are perfectly normal. The two architectures are compiled and linked separately and then compiled into a "fat" executable. Loosely, armv6 runs on "old" devices (pre-3GS) and armv7 runs on "new" devices (3GS+). "New" devices can also run armv6, but armv7 is much faster.)
精彩评论