链接器命令失败,退出代码1错误

时间:2013-10-28 03:14:55

标签: xcode compiler-errors

在尝试编译和运行我的项目时,我遇到了上面的错误。这是一个更详细的外观:

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:错误:链接器命令失败,退出代码为1(使用-v查看调用)

如何解决此问题?提前谢谢!

更新:以下是使用timeTick的3个文件:

·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

最后,有一个单独的文件可以将timeTick保存并加载到Core Data中。

的.m:

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

1 个答案:

答案 0 :(得分:1)

正在发生的事情是#import .h文件的每个文件现在都有自己的名为timeTick的变量。您需要做的是将其作为标题中的外部,然后在.m文件中定义它。所以你.h应该是这样的:

extern int timeTick;

然后你的.m应该在文件的顶部:

int timeTick = 0;

然后任何需要访问它的文件都会#import ".h"并查看定义。由于它是外部的,因此它们不会创建自己的timeTick,但会在链接时查找一个,它们会在.m文件中找到。