iOS programming - Duplicate symbol _OBJC_IVAR
operator class:
#import <Foundation/Foundation.h>
@interface operator : NSObject {
int number;
}
@property int number;
@end
@implementation operator
- (id)init{
self = [super init];
if (self) {
[self setNumber:0];
}
return self;
}
@synthesize number;
@end
main.m:
#import <UIKit/UIKit.h>
#import "operator.m"
int main(int argc, char *argv[]) {
id operator1 = [[operator alloc] init];
id operator2 = [[operator alloc] init];
[operator1 setNumber:10];
[operator2 setNumber:20];
int answer = [operator1 number] + [operator2 number];
printf("The answer is %d",answer);
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
I get an error -> ld: duplicate symbol _OBJC_IVAR_$_operator.number in /Volumes/Home/Desktop/testing/build/testing.build/Debug-iphonesimulator/testing.build/O开发者_Python百科bjects-normal/i386/operator.o and /Volumes/Home/Desktop/testing/build/testing.build/Debug-iphonesimulator/testing.build/Objects-normal/i386/main.o
This is my very first time I program in ObjC. Am I doing something wrong?
I tried the "Clean all targets" fix that I found on google but did not help.
- You should never
#import
an.m
file into another file. You import the.h
file, if it's needed. - You should not have code executing in
main
before you create the autorelease pool. That is going to cause problems sooner or later. In this case, you test code should probably go inapplication:didFininshLaunching
instead.
Static library involved
I added a class that had exactly the same name as a class in the static library that I also used. So adding a prefix to the name of my class solved the problem.
Search from an .m file that you can be importing in any file. Some times it is hard to find it
For other users where the above solutions were not the case this was what was wrong with mine. I'm a lazy programmer so instead of rewriting all the code for a new page (UIView) I will often copy and paste a previous UIView's .m and .h files... In doing so I sometimes forget to change the name of the interface inside the .m and .h files that I copied (even if I change the file names)... This will cause this same problem as well. Good luck to everyone else this happened too!
In my case, i found a double declaration on a .m file in my Build Phases --> Compile Sources. Deleting the duplicate solved the problem for me. Hope this helps.
精彩评论