简体   繁体   English

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

[英]Calling C function in Objective-C block (Linker error)

I have created C function: 我创建了C函数:

header 标头

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

implementation 实作

//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;
}

When i'm calling AddSkipBackupAttributeToItemAtURL from another parts of application everything is OK, except one place where i'm calling function from the block. 当我从应用程序的另一部分调用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];
    };

In that place ,while linking in progress, there is error: 在那个地方,进行链接时出现错误:

Undefined symbols for architecture i386: 体系结构i386的未定义符号:
" AddSkipBackupAttributeToItemAtURL", referenced from: _ _36-[UpdateFileAsyncOperation start]_block_invoke in UpdateFileAsyncOperation.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) AddSkipBackupAttributeToItemAtURL”,引用自:_ _36- [UpdateFileAsyncOperation.o中的[UpdateFileAsyncOperation start] _block_invoke

I don't understand why it happens, how it depends on block? 我不明白为什么会发生,它如何取决于阻止? and how can I fix that? 我该如何解决? Thanks! 谢谢!

Update: it is not dependent on block, i have moved calling of function to another place of class: the error still there. 更新:它不依赖于块,我已经将函数的调用移到另一个类的位置:错误仍然存​​在。 I'm trying to find why 我试图找出原因

I have solved the problem. 我已经解决了问题。 It not dependent on block. 它不依赖于块。 It dependent on file extensions. 它取决于文件扩展名。

The problem file was with ".m" extension, other files was with ".mm" extension. 问题文件的扩展名为“ .m”,其他文件的扩展名为“ .mm”。

So I have putted next macro in FileSystem.h file 所以我把下一个宏放在FileSystem.h文件中

#ifdef __cplusplus
extern "C" {
#endif

BOOL AddSkipBackupAttributeToItemAtURL(NSURL *url);

#ifdef __cplusplus
}
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM