-1

I just made the jump from Xcode 2 to Xcode 4 and discovered that header importing doesn't work. E.g. if I make a header file that defines a single variable, foo, then I import the header into Class A.h and Class B.h then my app fails to compile with the linker error:

duplicate symbol _foo in:
/Users/myself/Library/Developer/Xcode/DerivedData/Testcrap-grlgbsgwysjmmzagvozfywcwafar/Build/Intermediates/Testcrap.build/Debug/Testcrap.build/Objects-normal/x86_64/Class A.o
/Users/myself/Library/Developer/Xcode/DerivedData/Testcrap-grlgbsgwysjmmzagvozfywcwafar/Build/Intermediates/Testcrap.build/Debug/Testcrap.build/Objects-normal/x86_64/Class B.o
ld: 1 duplicate symbol for architecture x86_64
-clang: error: linker command failed with exit code 1 (use -v to see invocation)

WTF? It's like Xcode 4 doesn't even know what import means. How do I fix this?

  • 1
    Could you share some code on how you `#import` your files? And scraps from the .h/.m "Class A" and "Class B"? – Can Jul 22 '13 at 00:42
  • 2
    `WTF? It's like Xcode 4 doesn't even know what import means` ... It's more likely that you don't know how to use it correctly ;) Make sure you import each others header only in their .m file, in the .h file use @class A (in B.h) and @class B (in A.h) – HAS Jul 22 '13 at 06:07
  • Heh, yeah. I probably should've been a little clearer. Class A and Class B are virgin subclasses of NSText, except for one alteration: I added '#import "fooheader.h"' to their .h files. Now, this _does_ work properly as long as I don't simultaneously declare and initialize foo in fooheader. 'int foo;' is good, 'int foo=0;' causes the linker error. HAS, I'm not trying to import Class A's header into Class B and vice versa. I'm trying to import fooheader.h into Class A and Class B. – user2605052 Jul 22 '13 at 08:12

2 Answers2

0

Try to make sure you didn't #import/#include any code files, make sure that they are all checked on the right hand column for building, and ensure you don't externally link with them at a later stage.

If you absolutely need to import/include the code files, then uncheck the file for building in the right hand column.

Hope this helps.

phyrrus9
  • 1,441
  • 11
  • 26
0

Seems like everything is legal, and in such case there should be a duplication symbol error. You are defining same symbol in two different translation units. The question is why XCode 2 didn't complain about it (may be some bug in outdated compiler/linker, eh?). Anyway to solve this kind of problem you should declare your variable in header (via extern) and define it in source file. Take a look at this question for details.
For the diff between declaration and definition take a look here.

Community
  • 1
  • 1
cody
  • 3,233
  • 1
  • 22
  • 25
  • No, there shouldn't be a duplication symbol error. Importing a header into multiple source files is legal in C and is supposed to be legal in Objective C(for crying out loud, it's what #import was specifically designed for). Among other things, it's the standard method for making global variables. That it doesn't work in Xcode 4 makes absolutely no sense and renders a ton of code un-compilable. This has to be a bug or some horribly wrong default setting. How is it fixed? I need to know. – user2605052 Jul 22 '13 at 08:03
  • As you described the situation: 'if I make a header file that defines a single variable, foo, then I import the header into Class A.h and Class B.h' - thats a duplicating symbol. `#import` directive will guarantee that same header will not included twice in the same translation unit (analog of guards and `#pragma once` with `#include`). Probably you should paste a bit of code. – cody Jul 22 '13 at 08:26