-1

I'm compiling my program, but at the linking phase, I get an error:

ld: 1 duplicate symbol for architecture x86_64

I have 2 different classes, and each one contains a variable in its .m file inside the @implementation (Of course I know which one). If I change the name of one of them, it works. in this link I see that what's declared in .m is "private", so I don't understand where this error comes from and how to fix it.

Note - to make my question clearer: Let's say I have a class EiffelTower and another class NiceGreenMonkey, They both are subclasses of NSObject, and they both have a variable mAge (in real life it's (NSMutableDictionary *) someData).

Where and how should I declare that variable (mAge) so I don't get the error?

Cœur
  • 37,241
  • 25
  • 195
  • 267
NGG
  • 109
  • 8
  • 1
    Could you add snippets of your code to show the problem? – Losiowaty Aug 25 '16 at 08:08
  • you have added two objects with same name in your projects. Just copy paste error to get actual object name or you can search urself in error log. – Gagan_iOS Aug 25 '16 at 08:12
  • This is a basic misconception about how C/Obj-C works. Header limits visibility, not privacy. Also there is a big difference between compilation and linking. – Sulthan Aug 25 '16 at 09:31
  • @Sulthan you are right, I said "when I compile" but it's after it, at the linking. Yes, I come from Java, and trying to understand visibility. – NGG Aug 25 '16 at 09:40
  • @NirGaiger The comment you have just given to `Gagan_iOS` is not the sort of thing you should be posting to someone who is trying to help you. Are question is clearly very **UNCLEAR** as people of voting as it is unclear. Please don't be negative to those who are attempting to help you otherwise you will get a reputation for this and you'll get no help at all. – Popeye Aug 25 '16 at 09:52
  • @Popeye you are right, sorry Gagan_iOS. – NGG Aug 25 '16 at 10:12

1 Answers1

0

As you have included no code this is only a guess: If you declare a global variable in an (Objective-)C file then the compiled binary will have that global variable as a public symbol. If you use the same name for globals in different files and then try to combine the compiled binary into the same application you will get a duplicate symbol error from the linked/loader (the ld in your error).

(Note that this is nothing to do with the visibility of the two variables at the language level, without taking steps to make the variables visible when compiling a file the compiler will not allow you to reference a variable in another file as it simply won't see it.)

To prevent a variable defined with global scope having a public symbol associated with it you can add the modifier static to its declaration. This limits the visibility of the variable to just that file, both at the language and compiled binary levels.

Response to Comment

Your variables are not instance variables, you would not get the error you have if they were. So my guess was wrong, you have instead incorrectly declared your instance variables. This is why you should always include your code when asking questions. All I can do is guess again. This is what instance variable declarations should look like:

@implementation SomeClass
{  // open brace indicates start of instance declarations
   int birdCount; // an instance variable
   // ...
}  // close brace, end of instance variables

// methods...

@end

Have you missed out the braces? If you have then you have declared global and not instance variables.

If this guess is still wrong edit your question and add your code! You are lucky the SO community hasn't closed your question, you need to help people help you.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thank you for your answer, but the static modifier will set the variable as a "class variable" and not "instance variable", so it will work, but what else would it mean? – NGG Aug 25 '16 at 10:10
  • @NGG - see edited answer – CRD Aug 25 '16 at 14:38