It was my impression that using #import would only import a file once per build, yet after trying to define a variable in a header, and then importing that header in two different source files, I get a duplicate symbol linker error for the variable. How is this possible?
+3
A:
#import
makes header to be included once per file, but not per build. So your variable is defined in every file where you import your header and as global variable is visible by default in whole project you get linker error.
Correct way to define a global variable (if it is what you want to do) is define it in implementation file and reference it with the key word external in other files.
Also defining variable in header as static will limit its visibility to the single file, so you will get no error, but likely not the result you want ;)
Vladimir
2009-12-15 15:38:49
I believe what you're saying. I got confused because according to wikipedia, it's once per build, unless I'm reading it wrong:http://en.wikipedia.org/wiki/Objective-C#.23importYour solution works perfectly. Thanks.
quano
2009-12-15 15:50:51
#import includes the file once per compilation unit, which in this case essentially means for each source file. I'm not aware of any per-build include directives.
sbooth
2009-12-16 04:35:07
Ok sbooth, thanks for the clarification. :)I see that the wikipedia article has been edited also. Nice!
quano
2009-12-16 18:00:54
+1
A:
import make sure the 'declarations' are included only once. Since you are defining a variable which is a 'definition' which make you get a duplicate symbol linker error.
To solve this error - use .h for only declaration and .m for definition
As Vladimir explained you can use extern
Girish Kolari
2009-12-16 04:28:14