带有宏定义的外部函数

3
我在尝试使用extern函数进行宏定义时,在Objective C中遇到了链接问题。有任何想法吗?
头文件用于帮助与设备版本进行比较。
extern NSString* getOperatingSystemVerisonCode();

#if TARGET_OS_IPHONE // iOS
#define DEVICE_SYSTEM_VERSION                       [[UIDevice currentDevice]      systemVersion]
#else // Mac
#define DEVICE_SYSTEM_VERSION                       getOperatingSystemVerisonCode()
#endif

#define COMPARE_DEVICE_SYSTEM_VERSION(v)            [DEVICE_SYSTEM_VERSION compare:v options:NSNumericSearch]
#define SYSTEM_VERSION_EQUAL_TO(v)                  (COMPARE_DEVICE_SYSTEM_VERSION(v) == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              (COMPARE_DEVICE_SYSTEM_VERSION(v) == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  (COMPARE_DEVICE_SYSTEM_VERSION(v) != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 (COMPARE_DEVICE_SYSTEM_VERSION(v) == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     (COMPARE_DEVICE_SYSTEM_VERSION(v) != NSOrderedDescending)

.mm file

NSString* getOperatingSystemVerisonCode()
{
    /*
     [[NSProcessInfo processInfo] operatingSystemVersionString]
     */
    NSDictionary *systemVersionDictionary =
    [NSDictionary dictionaryWithContentsOfFile:
     @"/System/Library/CoreServices/SystemVersion.plist"];

    NSString *systemVersion =
    [systemVersionDictionary objectForKey:@"ProductVersion"];
    return systemVersion;
}

链接错误:
Undefined symbols for architecture x86_64:
  "_getOperatingSystemVerisonCode", referenced from:
      -[Manager isFeatureAvailable] in Manager.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
1个回答

6
问题并非由宏定义引起。 getOperatingSystemVerisonCode() 函数在一个“.mm”文件中被定义,并且因此编译为 Objective-C++。特别地,函数名作为 C++ 函数造成了名称混淆。但是当从 (Objective-)C 源代码使用时,期望的是未经过名称混淆的名称。
你有两个解决问题的选择:
  • Rename the ".mm" file to ".m", so that it is compiled as an Objective-C file.

  • In the header file where the function is declared, add the extern "C" declaration to enforce C linkage even in an (Objective-)C++ file:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    NSString* getOperatingSystemVerisonCode();
    
    #ifdef __cplusplus
    }
    #endif
    

有关混合使用C和C ++的更多信息,您可以参考以下链接:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接