如何从Objective C调用我的Qt/C ++ Dylib? [英] How To Call my Qt/C++ Dylib from Objective C?

查看:121
本文介绍了如何从Objective C调用我的Qt/C ++ Dylib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Qt/C ++编译了一个dylib.我创建了一个名为test()的简单类方法,该方法读取字符串输入并返回带有"-response"的字符串输出.现在如何在XCode 7(默认的Cocoa应用程序)的Objective C中加载该对象,并使其通过NSLog()发出test()?

I've compiled a dylib in Qt/C++. I created a simple class method called test() that reads a string input and returns a string output with "-response" back. Now how do I load that inside Objective C in XCode 7 (a default Cocoa application) and make it emit test() via NSLog()?

这是我的构建文件夹在Qt/C ++中的样子.

This is what my build folder looks like in Qt/C++.

推荐答案

您需要使用Objective-C ++类,它是Objective-C和C ++的混合体.

You need to use an Objective-C++ class, which is a hybrid of Objective-C and C++.

在很大程度上是Objective-C项目中使用一个或多个Objective-C ++类的最大挑战是避免将C ++类暴露给Objective-C类.因此,您需要在Objective-C ++头文件中避免使用C ++,而只需在实现文件中包含C ++.

The greatest challenge using one or more Objective-C++ classes in a largely Objective-C project is avoiding exposing C++ classes to the Objective-C classes. Therefore you need to avoid C++ in the Objective-C++ header file and just include the C++ in the implementation file.

例如:

CppWrapper.h:

CppWrapper.h:

#import <Foundation/Foundation.h>

@interface CppWrapper : NSObject
- (BOOL)callCppMethodA:(int)param;
@end

CppWrapper.mm:

CppWrapper.mm:

#import "CppWrapper.h"
#import "cppclass.h"    // C++

@interface CppWrapper()
{
    cppclass *_cppclass;    // C++
}
@end

@implementation CppWrapper

- (id)init
{
    self = [super init];
    if (self) {
        _cppclass = new cppclass();    // C++
    }
    return self;
}

- (void)dealloc
{
    delete _cppclass;    // C++
}

- (BOOL)callCppMethodA:(int)param
{
    return _cppclass->methodA(param);    // C++
}

@end

像这样使用它:

CppWrapper *cppWrapper = [CppWrapper new];
[cppWrapper callCppMethodA:123];

这篇关于如何从Objective C调用我的Qt/C ++ Dylib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆