NGG NGG - 4 months ago 11
iOS Question

How can a .m file see a variable delcared in another .m file?

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
(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 dont understand where this error comes from and how to fix it.

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

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


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...


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.