如何从C ++文件调用Objective C头文件?

时间:2017-01-27 02:02:50

标签: c++ ios objective-c

我希望从C ++文件中调用Objective C函数。当我将标题包含在C ++文件(Algebra.cpp)中的Objective C文件(UnZip.h)时,构建失败了吗?我该怎么办呢?如果我在Algebra.cpp中删除对Math.h的任何引用,那么一切都很好。我无法将Algebra.cpp的名称更改为Algebra.mm,因为它在其他地方的多个地方被使用。

File Math.h

void MathFormula(const char *sourceData, const char *answer);

File Math.m

#import <Foundation/Foundation.h>
#import "MathDocument.h"
void MathFormula(const char *sourceFile, const char *answer)
{
    NSString * sFile = [NSString stringWithUTF8String:sourceFile];
    NSString * ans = [NSString stringWithUTF8String:answer];
    MathDocument * mathDocument = [[MathDocument alloc] initWithPath:sFile];
    [mathDocument runFormula:ans];
}

Algebra.cpp

#include ...
#include Math.h
{
  ---
  const char * sourceFile = "/Users/u1/pckg.zip";
  const char * destPath = "/Users/u1";
  MathFormula(sourceFile, destPath);
  ---
}

1 个答案:

答案 0 :(得分:0)

(我假设您在Xcode中这样做了吗?)

1)“查看”菜单 - &gt; “导航员” - &gt; “显示项目导航器”(cmd-1)

2)在Navigator窗格(项目窗口的左侧)中选择您的文件()

3)“查看”菜单 - &gt; “公用事业” - &gt; “显示文件检查器”(cmd-option-1)

4)在Utilities窗格(项目窗口的右侧)中,顶部的第二个控件应为“Type”,其值可能为:“Default - C ++ Source”;将其更改为“Objective-C ++ Source”。

Xcode现在将该文件构建为Object-C ++文件。完成! (并完成了!)