在Objective-C块中调用C函数(链接器错误)

时间:2013-08-24 12:27:46

标签: iphone ios objective-c c linker

我创建了C函数:

//FileSystem.h file
#import <Foundation/Foundation.h>
BOOL AddSkipBackupAttributeToItemAtURL(NSURL *url);

实施

//FileSystem.mm
#import "FileSystem.h"
#import <sys/xattr.h>
#import <Support/Support.h>

static const char* attrName = "com.apple.MobileBackup";
BOOL AddSkipBackupAttributeToItemAtURL(NSURL *url) {
    BOOL operationResult = NO;
    // some implementation
    return operationResult;
}

当我从应用程序的其他部分调用AddSkipBackupAttributeToItemAtURL时,一切正常,除了我从块中调用函数的一个地方。

__block UpdateFileAsyncOperation* selfOperation = self;
    _contentDownloader.completionBlock = ^(NSData* data) {
        [data saveForcedToPath:selfOperation.filePath];
        NSURL* fileUrlWithScheme = [NSURL fileURLWithPath:selfOperation.filePath];
        AddSkipBackupAttributeToItemAtURL(fileUrlWithScheme);
        [runLoop removePort:port forMode:NSDefaultRunLoopMode];
        [selfOperation completeOperation];
    };

在那个地方,在进行链接时,有错误:

  

架构i386的未定义符号:
  “ AddSkipBackupAttributeToItemAtURL”,引自:         UpdateFileAsyncOperation.o中的_ _36- [UpdateFileAsyncOperation start] _block_invoke ld:找不到架构的符号   i386 clang:错误:链接器命令失败,退出代码为1(使用-v to   见调用)

我不明白为什么会发生这种情况,它取决于阻止?我该如何解决这个问题? 谢谢!

更新:它不依赖于块,我已将函数调用移到另一个类的地方:错误仍然存​​在。我想找到原因

1 个答案:

答案 0 :(得分:7)

我已经解决了这个问题。 它不依赖于块。它取决于文件扩展名。

问题文件的扩展名为“.m”,其他文件的扩展名为“.mm”。

所以我在FileSystem.h文件中推出了下一个宏

#ifdef __cplusplus
extern "C" {
#endif

BOOL AddSkipBackupAttributeToItemAtURL(NSURL *url);

#ifdef __cplusplus
}
#endif