PanicDev
PanicDev

Reputation: 325

Linker command failed with exit code 1 error

While attempting to compile and run my project, I ran into the error above. Here is a more detailed look:

duplicate symbol _timeTick in:
/Users/zstephen/Library/Developer/Xcode/DerivedData/MyStore_2-ejjgywkgxdulkogcohzbzojgqpzp/Build/Intermediates/MyStore 2.build/Debug-iphonesimulator/MyStore.build/Objects-normal/i386/TimeController.o
/Users/zstephen/Library/Developer/Xcode/DerivedData/MyStore_2-ejjgywkgxdulkogcohzbzojgqpzp/Build/Intermediates/MyStore 2.build/Debug-iphonesimulator/MyStore.build/Objects-normal/i386/DeviceDetailViewController.o
ld: 1 duplicate symbol for architecture i386

clang: error: linker command failed with exit code 1 (use -v to see invocation)

How do I fix this issue? Thanks in advance!

UPDATE: Here is the 3 files timeTick is used:

.h:

#import <UIKit/UIKit.h>

int timeTick = 0;

@interface TimeController : UIViewController{
IBOutlet UILabel *labelTime;
}
- (IBAction)startTimer:(id)sender;


@end

.m:

@implementation TimeController


NSTimer *timer;   

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    labelTime.text = @"0";
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    - (IBAction)startTimer:(id)sender {
    [timer invalidate];
    timer= [NSTimer scheduledTimerWithTimeInterval:60.0 target:(self) selector:             (@selector(tick)) userInfo:(nil) repeats:(YES)];
}

-(void)tick{
    timeTick++;
    NSString *timeString = [[NSString alloc] initWithFormat:@"%d", timeTick];
    labelTime.text = timeString;



}


@end

Finally, there is a separate file that saves and loads timeTick into Core Data.

.m:

NSNumber *timetickNumber = [NSNumber numberWithInt:timeTick];
[newDevice setValue:timetickNumber forKey:@"name"];

Upvotes: 1

Views: 1087

Answers (1)

user1118321
user1118321

Reputation: 26325

What's happening is that every file that #imports the .h file now has it's own variable named timeTick. What you need to do is make it an external in the header, then define it in the .m file. So you're .h should look like this:

extern int timeTick;

Then your .m should have this at the top of the file:

int timeTick = 0;

Then any file which needs access to it would #import ".h" and see the definition. Since it's external they won't create their own timeTick but will look for one at link time, which they'll find in the .m file.

Upvotes: 1

Related Questions