Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS programming - Duplicate symbol _OBJC_IVAR

Tags:

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

like image 688
AlexBrand Avatar asked Aug 14 '10 16:08

AlexBrand


2 Answers

  1. You should never #import an .m file into another file. You import the .h file, if it's needed.
  2. 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 in application:didFininshLaunching instead.
like image 192
Jakob Borg Avatar answered Oct 11 '22 06:10

Jakob Borg


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.

like image 29
DanSkeel Avatar answered Oct 11 '22 04:10

DanSkeel