映射一个类型定义的数组
SWIG: Mapping an array of a typedef
我正在使用SWIG为一些c++类创建Ruby Wrapper。这是给我带来麻烦的c++方法的签名:
virtual LogP wordProb(VocabIndex word, const VocabIndex *context);
这是VocabIndex的定义:
#ifdef USE_SHORT_VOCAB
typedef unsigned short VocabIndex;
#else
typedef unsigned int VocabIndex;
#endif
这是我在Ruby脚本中调用它的方式:
index = 8
context = [index]
puts ngram.wordProb(index, context)
这是我运行脚本时得到的错误:
ngram.rb:26:in `wordProb': Expected argument 2 of type VocabIndex const *, but got Array [8] (TypeError)
in SWIG method 'wordProb'
from ngram.rb:26:in `<main>'
My attempt solution:
阅读文档后(是的,我使用SWIG 2.0),我在我的。I文件中尝试了这个:
%module rubylm
%{
#include "srilm-1.7.1/lm/src/Ngram.h"
%}
%include "srilm-1.7.1/lm/src/Counts.h"
%include "srilm-1.7.1/lm/src/Ngram.h"
%include "typemaps.i"
virtual LogP Ngram::wordProb(VocabIndex word, const VocabIndex *INPUT);
swig命令运行得很好,但是当我试图构建包装器库时,我得到了这个:
NgramWrapper_wrap.cxx:148:17: fatal error: tcl.h: No such file or directory
#include <tcl.h>
所以我启动了一个终端(这是一个Ubuntu盒子)并运行:
sudo apt-get install tcl-dev
这个命令安装了tcl 8.6,它把它的头文件放在/usr/include/tcl8.6
目录下。所以我在Makefile行中添加了include目录,它构建了NgramWrapper_wrap.o:
NgramWrapper_wrap.o: NgramWrapper_wrap.cxx
$(CC) $(CFLAGS) NgramWrapper_wrap.cxx -I $(RUBY_SRC) -I $(MISC_INCLUDE) -I $(DSTRUCT_INCLUDE) -I /usr/include/tcl8.6
然而,我仍然得到构建错误。这就是我被难住的地方:
NgramWrapper_wrap.cxx:10812:34: error: ‘RARRAY_LEN’ was not declared in this scope
int size = RARRAY_LEN(objv[3]);
^
NgramWrapper_wrap.cxx:10816:5: error: ‘VALUE’ was not declared in this scope
VALUE *ptr = RARRAY_PTR(objv[3]);
^
NgramWrapper_wrap.cxx:10816:12: error: ‘ptr’ was not declared in this scope
VALUE *ptr = RARRAY_PTR(objv[3]);
^
NgramWrapper_wrap.cxx:10816:36: error: ‘RARRAY_PTR’ was not declared in this scope
VALUE *ptr = RARRAY_PTR(objv[3]);
^
NgramWrapper_wrap.cxx:10819:35: error: ‘StringValuePtr’ was not declared in this scope
arg3[i]= StringValuePtr(*ptr);
^
NgramWrapper_wrap.cxx: In function ‘int _wrap_NgramCountWrapper_run(ClientData, Tcl_Interp*, int, Tcl_Obj* const*)’:
NgramWrapper_wrap.cxx:10908:34: error: ‘RARRAY_LEN’ was not declared in this scope
int size = RARRAY_LEN(objv[3]);
^
NgramWrapper_wrap.cxx:10912:5: error: ‘VALUE’ was not declared in this scope
VALUE *ptr = RARRAY_PTR(objv[3]);
^
NgramWrapper_wrap.cxx:10912:12: error: ‘ptr’ was not declared in this scope
VALUE *ptr = RARRAY_PTR(objv[3]);
^
NgramWrapper_wrap.cxx:10912:36: error: ‘RARRAY_PTR’ was not declared in this scope
VALUE *ptr = RARRAY_PTR(objv[3]);
^
NgramWrapper_wrap.cxx:10915:35: error: ‘StringValuePtr’ was not declared in this scope
arg3[i]= StringValuePtr(*ptr);
我能想到的是Ruby, Swig和Tcl之间的版本不匹配。但是我怎么知道使用哪个Tcl版本呢?
嗯。
我刚刚做了以下操作
vocal.i
%module rubylm
%{
#include "Ngram.h"
%}
%include "Ngram.h"
%include "typemaps.i"
virtual LogP Ngram::wordProb(VocabIndex word, const VocabIndex *INPUT);
Ngram.h
#pragma once
#ifdef USE_SHORT_VOCAB
typedef unsigned short VocabIndex;
#else
typedef unsigned int VocabIndex;
#endif
typedef int LogP;
class NGram {
public:
LogP wordProb(VocabIndex word, const VocabIndex *context);
};
命令执行 swig2.0 -ruby -c++ vocal.i
g++ -c vocal_wrap.cxx -I/usr/include/ruby-2.1.0 -I/usr/include/x86_64-linux-gnu/ruby-2.1.0
没有任何错误。您是否忘记了-c++选项以及为什么需要tcl.h
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 列表参数的类型定义
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 关于 C++ 中的函数类型定义
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 将使用/类型定义限制为类范围
- 模板类型定义?
- C++:模板类的类型定义
- 如何对命名空间限定类型进行类型定义?
- 此递归模板类型定义是否有效C++?
- 具有调整对齐方式的类型定义
- C++从抽象类型定义类成员
- 用于C++代码的 API 监视器类型定义 (XML)
- 如何将result_of与函数类型定义一起使用
- 在C++的适当类型定义位置
- 如何根据模板类型定义浮点常量?
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 参数化类的别名(或类型定义)内部类
- 如果我想从类型"T"定义元素的容器(来自 STL),那么"T"必须使用默认构造函数?