如何在.m文件和.mm文件(obj c和obj c++)之间共享通用头文件的全局函数?

问题描述 投票:0回答:1

abc.h

/**
 @function  GetInputSizeFromUsage
 @abstract  return frame width and height from usage
 @param     usage   PPVEUsage to be inqure
 @param     width   (out) the input width for usage
 @param     height  (out) the input height for usage
 */
void GetInputSizeFromUsage(PPVEUsage usage, size_t *width, size_t *height);

文件1.m

#import <abc.h>
void GetInputSizeFromUsage(PPVEUsage usage, size_t *width, size_t *height)
{
    switch(usage) {
        case UsageLandscape848x480:
            *width = 848;
            *height = 480;
            break;

        default:
            *width = 0;
            *height = 0;
            break;
    }

}

// This file uses this above function in various other functions and is fine to compile

文件2.mm

#import <abc.h>

size_t GetInputSizeFromUsage(PPVEUsage usage)
{
    size_t width, height;
    GetInputSizeFromUsage(usage, &width, &height);
    return (width * height);
}

错误: 未定义符号:GetInputSizeFromUsage(PPVEUsage, unsigned long*, unsigned long*)

该错误是由于 file2.mm 无法看到该符号造成的。如果我在 file2.mm 中将其注释掉,它可以正常编译。

期望它能够看到符号

objective-c xcode objective-c++
1个回答
0
投票

Objective-C++ 是 C++ 语言的超集,C++ 的所有规则都适用于此。 IE。声明函数时,您必须处理所谓的“名称修改”,它会以编译器无法在 Objective-C/C 编译单元中找到它的方式更改函数签名。对于这种情况,您应该通过使用 extern "C" 部分告诉链接器在将此标头包含在 C++ 编译单元中时应用 C 规则:

#ifdef __cplusplus
extern "C" {
#endif
void GetInputSizeFromUsage(PPVEUsage usage, size_t *width, size_t *height);
#ifdef __cplusplus
}
#endif

© www.soinside.com 2019 - 2024. All rights reserved.