gpt4 book ai didi

objective-c - 带有宏的外部函数

转载 作者:搜寻专家 更新时间:2023-10-30 20:00:02 25 4
gpt4 key购买 nike

当我尝试使用外部函数执行宏时,我在 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文件

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)

最佳答案

问题不是宏定义引起的。

getOperatingSystemVerisonCode() 函数在“.mm”文件中定义,因此编译为 Objective-C++。特别是,函数名称被破坏为 C++ 函数。但是,当从 (Objective-)C 来源引用时,需要未损坏的名称。

你有两种选择来解决这个问题:

  • 将“.mm”文件重命名为“.m”,以便将其编译为 Objective-C 文件。

  • 在声明函数的头文件中,添加 extern "C" 声明以强制执行 C 链接,即使在 (Objective-)C++ 文件中也是如此:

    #ifdef __cplusplus
    extern "C" {
    #endif

    NSString* getOperatingSystemVerisonCode();

    #ifdef __cplusplus
    }
    #endif

有关混合 C 和 C++ 的更多信息,请参阅示例

关于objective-c - 带有宏的外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23882780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com