Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIG:映射typedef的数组 我用SWIG创建一些C++类的Ruby包装器。这是C++方法的签名,给我带来麻烦: virtual LogP wordProb(VocabIndex word, const VocabIndex *context);_C++_Ruby_Linux_Pointers_Swig - Fatal编程技术网

SWIG:映射typedef的数组 我用SWIG创建一些C++类的Ruby包装器。这是C++方法的签名,给我带来麻烦: virtual LogP wordProb(VocabIndex word, const VocabIndex *context);

SWIG:映射typedef的数组 我用SWIG创建一些C++类的Ruby包装器。这是C++方法的签名,给我带来麻烦: virtual LogP wordProb(VocabIndex word, const VocabIndex *context);,c++,ruby,linux,pointers,swig,C++,Ruby,Linux,Pointers,Swig,这是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

这是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>'
swig命令运行良好,但当我尝试构建包装器库时,我得到了以下结果:

NgramWrapper_wrap.cxx:148:17: fatal error: tcl.h: No such file or directory
 #include <tcl.h>
这安装了tcl 8.6,它将头文件放在
/usr/include/tcl8.6
目录中。因此,我在构建NgramWrapper_wrap.o的Makefile行中添加了include目录:

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版本呢?我搜遍了所有的文件都没有用…

我只是做了下面的事情

声音,我

%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

Carrays。我是一个快速的解决方案:拥有比我更多的ruby知识,你可以做一些更聪明的事情。如果我尝试使用Carrays.i,我会得到与尝试使用typemaps.i时相同的构建错误。我的Ruby Swig Tcl组合有点问题。我现在能想到的就是降级Tcl。我要试试。当我%include
typemaps.I
carrays.I
时,Tcl依赖项就会出现。如果我看一下生成的代码,在开头有一行:
#define SWIGTCL
。-c++选项始终存在,我使用的是Makefile。顺便说一下,您使用的是ruby 2.1.0,我使用的是ruby 1.9.3。这可能是个问题吗?也许吧。我想我会仔细查看包含的标题,看看是否有一些缺少的定义。记住SWIG不会递归头。也有可能是错误的类型映射。我被包括在内。@dario_ramos我在生成的代码中没有
#define SWIGTCL
。我想可能包含的
typemaps.I
carrays.I
是错误的。你在包装Ruby的代码有没有办法让我检查一下?或者在%include指令中使用绝对路径。。。我将尝试验证参数-ruby是否传递给SWIG而不是-tcl。在swig目录中检查ruby文件夹中的
typemaps.i
是否正确。
%module rubylm

%{
#include "Ngram.h"
%}

%include "Ngram.h"
%include "typemaps.i"

virtual LogP Ngram::wordProb(VocabIndex word, const VocabIndex *INPUT);
#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);
};