带宏的外部函数 [英] extern function with macro
问题描述
当我尝试使用extern函数执行marco时,在目标C中存在链接器问题.知道为什么吗?
Im having a linker problem in Objective C when i attempt to do a marco with a extern function. Any idea why?
头文件
为了协助与设备版本进行比较
Header file
To assist in doing comparison with the device version
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)
推荐答案
问题不是由宏定义引起的.
The problem is not caused by the macro definition.
getOperatingSystemVerisonCode()
函数在".mm"文件中定义,因此
编译为Objective- C ++ .特别是,函数名称被改成C ++函数.
但是,从(Objective-)C来源引用时,应该使用不易混淆的名称.
The getOperatingSystemVerisonCode()
function is defined in a ".mm" file and therefore
compiled as Objective-C++. In particular, the function name is mangled as a C++ function.
But when referenced from (Objective-)C sources, the unmangled name is expected.
您可以通过两种方法解决该问题:
You have two options to solve the problem:
-
将".mm"文件重命名为".m",以便将其编译为Objective-C文件.
Rename the ".mm" file to ".m", so that it is compiled as an Objective-C file.
在声明函数的头文件中,添加extern "C"
声明以强制C链接,即使在(Objective-)C ++文件中也是如此:
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 ++的更多信息,请参见
For more information about mixing C and C++, see for example
这篇关于带宏的外部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!