关于ios:如何为c方法编写objective c wrapper? | 珊瑚贝

How to write objective c wrapper for c++ methods?


我需要在objective C 中为 C 类编写一个package类。

我在 Cocoa 项目中使用 C 类时参考了以下 Can\\’t find standard C includes,并且能够摆脱词法或预处理器问题:\\’vector\\’ file not found 问题。

但是,我不明白将接受多个参数的 C 方法转换为objective C 方法。

有人可以帮我这样做吗?我想做的是为此 http://breakfastquay.com/rubberband/code-doc/classRubberBand_1_1RubberBandStretcher.html#a0af91755d71eecfce5781f2cd759db85

编写一个package类

我已经尝试过这样做,以下是我坚持使用的方法…

1
2
3
4
5
6
7
8
9
10
11
//  Wrapper.h

#import <Foundation/Foundation.h>

@interface Wrapper : NSObject {
    void *myRubberBandStretcher;
}

#pragma mark – Member Functions
(void)process:(const float)input samples:(size_t)samples final:(bool)final;
@end

/////////////////////////////////////// /////////////////////////////

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Wrapper.mm

#import”Wrapper.h”
#import”RubberBandStretcher.h”

@implementation Wrapper

(id)init {
self = [super init];
if (self) {
   myRubberBandStretcher = new RubberBand::RubberBandStretcher::RubberBandStretcher(44100, 2, 0, 1.0, 1.0);
}
return self;
}

(void)process:(const float)input samples:(size_t)samples final:(bool)final {
static_cast<RubberBand::RubberBandStretcher *>(myRubberBandStretcher)->process(<#const float *const *input#>, <#size_t samples#>, <#bool final#>)
}

  • 我猜你不需要在 Objective-C 中为 C 创建特殊的package类。只需将实现文件扩展名从 .m 更改为 .mm,您的 C 代码就会开始编译和运行。
  • 我为橡皮筋库创建了静态库,并且只得到了 RubberBandStretcher.h 文件。头文件是用 C 编写的。当我导入头文件并转到头文件并给我”词法或预处理器问题:\\’vector\\’文件未找到问题”错误。我已经摆脱了这个stackoverflow.com/questions/6083764/之后的错误…所以我想我需要写一个package器
  • 您可以将使用 c 语法的文件扩展名更改为 .mm 吗?那么就没有必要纠正package了。
  • @Arjuna 正如我上面所说,我只有头文件和静态库。头文件扩展名为 .h。没有 .m 文件我正在为它编写package类并且有 .h 和 .mm 文件。我在上面的问题中包含了 .h 文件和 .mm 文件,但对如何转换早餐quay.com/rubberband/code-doc/… 文件中可用的 c 方法感到困惑
  • @Arjuna我试图转换为objective C的方法就是这种方法; void RubberBand::RubberBandStretcher::process\\\\t(\\\\tconst float *const * \\\\tinput, size_t \\\\tsamples, bool \\\\tfinal ) 我不知道如何将其转换为objective C,因为它需要几个参数方法。如果您需要查看完整代码,我可以为您上传项目。谢谢
  • 如果将 const float *const * 作为输入类型会发生什么?
  • 当我按照你的建议写它时,它看起来像这样 -(void)process:(const float *const *)input samples:(size_t)samples final:(bool)final { static_cast<RubberBand::RubberBandStretcher *>(myRubberBandStretcher) – > process(<const float *const *input>, <size_t samples>, <bool final>); } 和 xcode 提示我一个错误说 “Expected expression”
  • 请上传您的项目。


您应该阅读”编译单元”。简而言之,没有办法告诉编译器用什么语言编写头文件。因此,它始终使用包含标头的源文件的语言。因此,如果您从 .m 文件中包含 C 标头,它将不起作用。对于每个使用 C 标头的 ObjC 文件,您必须将使用它的源文件的文件名后缀从 .m 更改为 .mm。您不需要橡皮筋库的源文件,您只需要制作所有使用它的文件 ObjC 而不是 ObjC。

当然,您可能不想将所有文件都更改为 ObjC 。您用于package C 的替代方法的代码非常接近。我注意到的一件事是您的代码中仍然有 <#foo#> 占位符(Xcode 可能会将它们显示为蓝色圆形圆形装饰,这可能是您以前没有注意到它们的原因)。将它们替换为参数变量的实际名称,就像使用常规 C 函数调用一样,并且都应该编译。即:

1
2
3
(void)process:(const float)input samples:(size_t)samples final:(bool)final {
    static_cast<RubberBand::RubberBandStretcher *>(myRubberBandStretcher)->process( input, samples, final );
}

另请注意,您似乎在 process 调用之后省略了分号。

顺便说一句,您还可以摆脱在任何地方都必须使用 static_cast 的麻烦,请参阅此处列出的技巧:我可以在编译和链接时将 C 主函数和类与 Objective-C 和/或 C 例程分开吗?这里可能还有一些更有用的信息:How to call C method from Objective-C Cocoa Interface using Objective-C


来源:https://www.codenong.com/28470882/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9415.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?