简体   繁体   English

通过 SWIG 从 Ruby 调用 C++ 函数

[英]Calling C++ function from Ruby via SWIG

I'm trying to call this function我正在尝试调用此函数

bool process(const short* pPCM, size_t num_samples, bool end_of_stream = false);

from fplib part of the Last.FM FingerprintExtractor development at http://github.com/lastfm/Fingerprinter来自http://github.com/lastfm/Fingerprinter 上Last.FM FingerprintExtractor 开发的 fplib 部分

I'm trying to call it from Ruby (1.9.2) going via SWIG (2.0.8).我试图通过 SWIG (2.0.8) 从 Ruby (1.9.2) 调用它。

This is my SWIG file so far到目前为止,这是我的 SWIG 文件

/* */
%define DOCSTRING
"This is a wrapper for The last.fm Fingerprint Extractor library."
%enddef
%module(docstring=DOCSTRING) FingerprintExtractor
%{
#include "../include/fplib/FingerprintExtractor.h"
%}
%include "typemaps.i"
%apply const short *INPUT { const short *pPCM };
%include "../include/fplib/FingerprintExtractor.h"

The output from swig -c++ -ruby FingerprintExtractor.i swig -c++ -ruby FingerprintExtractor.i 的输出

FingerprintExtractor.i:10: Warning 453: Can't apply (short const *INPUT). No typemaps are defined.

I think the problem I'm having is a around the "const".我认为我遇到的问题是围绕“const”。 Unfortunately I'm new to C++ and SWIG.不幸的是,我是 C++ 和 SWIG 的新手。 I understand that the "const" forces the variable to be a constant, which is good practice in C++.我知道“const”强制变量为常量,这在 C++ 中是一种很好的做法。 But I can't figure out how to get SWIG to play ball.但我不知道如何让 SWIG 打球。 As per the message I need to define a typemap for the "const short *" declaration but for the life of me I can't figure it out.根据消息,我需要为“const short *”声明定义一个类型映射,但对于我的生活,我无法弄清楚。

Any help in figuring this out would be appreciated.任何帮助解决这个问题将不胜感激。

I've read the extensive documentation at http://www.swig.org/Doc1.3/Ruby.html which is sadly beyond my skills and whilst it covers aspects on pointers, arrays, constants, typedefs and typemaps I haven't been able figured out how all this relates to passing a pointer to an array when its declared as a const.我已经阅读了http://www.swig.org/Doc1.3/Ruby.html 上的大量文档,可悲的是,这超出了我的技能范围,虽然它涵盖了关于指针、数组、常量、typedef 和 typemaps 的方面我还没有当数组声明为常量时,能够弄清楚所有这些与将指针传递给数组的关系。 I've have reviewed the C++ language to understand the requirement for the "const".我已经查看了 C++ 语言以了解“const”的要求。 I've tried to further understand the situation by looking at others implementations and of course I've googled it.我试图通过查看其他实现来进一步了解情况,当然我已经用谷歌搜索过了。 Nothing specific enough turned up at a level I could understand.在我能理解的水平上没有出现足够具体的内容。 Hence my request for assistance.因此,我请求帮助。

Try adding:尝试添加:

%include "stdint.i" %include "stdint.i"

underneath:下:

%include "typemaps.i" %include "typemaps.i"

stdint.i has typedefs for common types including short. stdint.i 具有用于包括 short 在内的常见类型的 typedef。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM