17

对于已经成功从Objective-C调用C++代码的各位,请赐教?

这个链接说你需要使用他在他的文章中描述的技术来包装你的 C++ 代码。它看起来不错,但我仍然有问题。

链接表明,只要调用 C++ 类的 Objective-C 类已转换为 .mm (Objective-C++) 类,那么两者应该可以很好地协同工作。

当我尝试添加方法调用时,这些方法中的每一个都让我感到悲伤。谁能给我一个简单的 Hello World iOS 应用程序的代码,该应用程序使用 Objective-C 作为“Hello”部分,使用 C++ 类作为“World”部分,中间有一个 Objective-C++ 类?还是我的整个概念仍然错误?

4

3 回答 3

24

本质上,您需要一个扩展名为 .mm 的 ObjC 类,它调用扩展名为 .mm 的 ObjC 类。第二个将用作 C++ 包装类。包装类将调用您实际的 .cpp 类。这有点棘手,所以我会给你一些详细的代码。以下是该项目的概述:

在此处输入图像描述

在您的 ObjC 代码 (ViewController) 中,您将调用 CplusplusMMClass

- (IBAction)buttonPushed:(UIButton *)sender {
    self.mmclass = [[CplusplusMMClass alloc]init];  // bad practice; but showing code
    NSString *str = [self.mmclass fetchStringFromCplusplus];
    [self populateLabel:str];
}

这是 CplusplusMMClass .h 和 .mm

#import <Foundation/Foundation.h>
#import "WrapperClass.h"

@interface CplusplusMMClass : NSObject
@end

@interface CplusplusMMClass()
@property (nonatomic, strong) WrapperClass *wrapper;
- (NSString*)fetchStringFromCplusplus;
@end

#import "CplusplusMMClass.h"
#import "WrapperClass.h"

@implementation CplusplusMMClass

- (NSString*)fetchStringFromCplusplus {
    self.wrapper = [[WrapperClass alloc] init];
    NSString * result = [self.wrapper getHelloString];
    return result;
}

@end

这是 WrapperClass .h 和 .mm

#ifndef HEADERFILE_H
#define HEADERFILE_H

#import <Foundation/Foundation.h>

#if __cplusplus

#include "PureCplusplusClass.h"

@interface WrapperClass : NSObject
@end

@interface WrapperClass ()
- (NSString *)getHelloString;
@end


#endif
#endif

#import "WrapperClass.h"

#include "WrapperClass.h"
#include "PureCplusplusClass.h"

using namespace test;

@interface WrapperClass ()
@property (nonatomic) HelloTest helloTest;
@end

@implementation WrapperClass

- (NSString *)getHelloString {
    self.helloTest = *(new HelloTest);
    std::string str = self.helloTest.getHelloString();
    NSString* result = [[NSString alloc] initWithUTF8String:str.c_str()];
    return result;
}

@end

这是 PureCplusplusClass .h 和 .cpp

#ifndef __HelloWorld__PureCplusplusClass__
#define __HelloWorld__PureCplusplusClass__

#include <stdio.h>
#include <string>

using namespace std;

namespace test {
    class HelloTest
    {
    public:
        std::string getHelloString();
    };
}

#endif /* defined(__HelloWorld__PureCplusplusClass__) */

#include <stdio.h>
#include <string>

std::string test::HelloTest::getHelloString() {
    std::string outString = "Hello World";
    return outString;
}

这段代码并不完美!我在识别命名空间测试时遇到问题。有空我会更新。

但这应该能让你到达那里!!!!

于 2015-04-08T22:05:53.870 回答
2

另一个(人为的)一个:

使用 C++ 类作为 ivar:

文件 Foo.h

#import <Foundation/Foundation.h> 

@interface Foo : NSObject
@property (nonatomic, readonly) NSString* what;
@end

文件:Foo.mm

#import "Foo.h"
#include <string>

@implementation Foo {
    std::string _string;  // C++ class must have a default c-tor
}

- (id)init
{
    self = [super init];
    if (self) {
        _string = "Hello, World!"
    }
    return self;
}

- (NSString*) what {
    NSString* result = [[NSString alloc] initWithBytes:_string.data()
                                                length:_string.size() 
                                              encoding:NSUTF8StringEncoding];
    return result;
}

@end

笔记:

可执行文件可能需要显式链接 C++ 库,例如通过向“其他链接器标志”添加附加标志:-lc++

或者,main.m可以将文件重命名为main.mm.

后者在选择“正确”库方面更加稳健,因为工具链会自行完成。但是,对于其他检查项目的人来说,重命名的“main.m”可能并不那么明显,并且可能会引起混淆。

于 2013-10-07T16:54:03.590 回答
-2

我在使用 xcode 12.4 时遇到了这种情况,需要在 Objective-C 项目中使用现有的 C++ 类“CppModule”。这就是我所做的。

  1. 第一个问题是 CppModule.hpp 中的#include 给出了编译器错误:找不到“字符串”文件。通过将 .hpp 文件内容包装在
#if defined __cplusplus
  declarations
#endif
  1. 然后 string var 声明给出了另一个编译器错误: Unknown type name 'string'; 你的意思是'std :: string'吗?通过以下方式修复:
#include <string> 
using namespace std;
  1. C++ CppModule.cpp、CppModule.hpp 包含文件、调用objective-c module.m 和它的module.h 文件必须全部设置为类型“Objective-C++ Source”,使用xcode 的检查器(编辑器右上角窗户)。将 module.m 重命名为 module.mm 还将类型设置为“Objective-C++ Source”(xcode 12.4)。使用 .mm 可能很好,提醒一下它调用 C++。

在 module.h 文件中包含 C++ 头文件:

#include < CppModule.hpp>
  1. 奇怪(对我来说)是在 module.h 文件中“CppModule”未被识别为已知类型,因此您不能在 module.h 中声明该类型的变量或属性,但在 module.m(m) 文件中它是一种已知类型。在 module.mm 文件中,在 @implementation 之后添加:
static CppModule *module_cpp;
  1. 现在您可以,例如在 module.mm 的 init 中,调用 C++ 模块的初始化程序,例如:
module_cpp = new CppModule(parameters);

并且 C++ 模块仍然可以通过该静态 module_cpp *pointer 访问。

  1. 在你的 Objective-C 方法中调用 module.cpp 的方法(示例):
CppModule::Result r = module_cpp->CppMethod();

看起来工作得很好!

如果有人能解释 4 中的奇怪之处,我将不胜感激。

我需要的模块只是做一些复杂的(数字)计算,我不想把那个代码转换成 Objective-C。

于 2021-04-18T22:00:20.327 回答