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

标签 c++ swig

我正在尝试调用此函数

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

来自 Last.FM FingerprintExtractor 开发的 fplib 部分 http://github.com/lastfm/Fingerprinter

我正在尝试通过 SWIG (2.0.8) 从 Ruby (1.9.2) 调用它。

这是迄今为止我的 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"

swig -c++ -ruby FingerprintExtractor.i 的输出

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

我认为我遇到的问题是围绕“const”的。不幸的是我是 C++ 和 SWIG 的新手。我知道“const”强制变量成为常量,这是 C++ 中的良好做法。但我不知道如何让 SWIG 发挥作用。根据消息,我需要为“const Short *”声明定义一个类型映射,但我一生都无法弄清楚。

任何解决这个问题的帮助将不胜感激。

我已阅读 http://www.swig.org/Doc1.3/Ruby.html 上的大量文档遗憾的是,这超出了我的技能范围,虽然它涵盖了指针、数组、常量、typedef 和类型映射方面的内容,但我无法弄清楚所有这些与将指针声明为 const 时将其传递给数组有何关系。我已经回顾了 C++ 语言以了解“const”的要求。我试图通过查看其他实现来进一步了解这种情况,当然我也用谷歌搜索过它。在我能理解的水平上没有出现足够具体的情况。因此我请求帮助。

最佳答案

尝试添加:

%包含“stdint.i”

下面:

%包含“typemaps.i”

stdint.i 具有常见类型(包括短类型)的 typedef。

关于c++ - 通过 SWIG 从 Ruby 调用 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335766/

相关文章:

c++ - 重写 if 语句,使其更简洁、更简洁

python - 有什么好的方法可以为 swig 接口(interface)生成文档吗?

java - 解析 SWIG 接口(interface)文件的结构属性

使用多维数组的 C++ 模板

c++ - 在 Linux 上与 Eclipse 交叉编译,在 Windows 上与 Eclipse 交叉编译?

c++ - SWIG_NewPointerObj 和值始终为零

ruby - 为什么我不能让 swig wrap std::vector 到 Ruby 类?

python - Python Swig 运行时出现问题

c++ - 将操作码打印为asm格式

c++ - 在 C++ 中增加自定义对象数组的大小