extern导致链接器错误

时间:2013-02-15 15:18:59

标签: ios objective-c extern

似乎几乎是this question的副本,但那里的答案没有解决问题。

first.h

extern PlanningDocumentService *folderService;

first.m

#import first.h

- (void)serviceDefinition
 {    
    PlanningDocumentService *folderService = [_myDocumentViews objectAtIndex:[_gridView indexOfSelectedItem]];   
    detail.service = folderService;
 }

second.h

#import first.h

second.m

#import second.h

+(NSArray*)getDocumentData
{
     NSString *passedCaption =  folderService.caption;
}

给出错误:

Undefined symbols for architecture i386:
  "_folderService", referenced from:
      +[GenericService getDocumentData] in GenericService.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

除了上面的文件之外,我还没有声明或定义folderService。 欢迎任何想法。

2 个答案:

答案 0 :(得分:4)

您需要将folderService定义为具有全局范围:

first.m:

#import "first.h"

PlanningDocumentService *folderService = nil;

- (void)serviceDefinition
{    
    folderService = [_myDocumentViews objectAtIndex:[_gridView indexOfSelectedItem]];   
    detail.service = folderService;
}

答案 1 :(得分:3)

PlanningDocumentService *folderService内的

- (void)serviceDefinition使变量本地化,但不是全局变量。但extern PlanningDocumentService *folderService声明存在一个永远不会被定义的全局变量。