gpt4 book ai didi

c++ - SWIG:映射 typedef 数组

转载 作者:IT王子 更新时间:2023-10-29 00:34:08 25 4
gpt4 key购买 nike

我正在使用 SWIG 为某些 C++ 类创建 Ruby 包装器。这是给我带来麻烦的 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>'

我尝试的解决方案:

看完docs (是的,我正在使用 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 目录中。所以我在构建 NgramWrapper_wrap.o 的 Makefile 行中添加了包含目录:

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 版本呢?我搜索了文档无济于事......

最佳答案

嗯。

我刚刚做了以下

声乐.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

关于c++ - SWIG:映射 typedef 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32686716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com