映射一个类型定义的数组

SWIG: Mapping an array of a typedef

本文关键字:类型 定义 数组 一个 映射      更新时间:2023-10-16

我正在使用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