如何为接受和返回 2 个表格的函数创建 SWIG typemap

这是我的 SWIG typemap:

%apply (float *INOUT, int) {(float *io1, int n1)};
%apply (float *INOUT, int) {(float *io2, int n2)};

这是我的函数:

void process(float *io1, int n1, float *io2, int n2)
    {
        for (int i = 0; i < n1; ++i)
        {
            io1[i] = io1[i] * 0.1;
            io2[i] = io2[i] * 0.1;
        }
    }

我预期 process 函数接受 2 个表格并返回 2 个表格。

在 Lua 中,process 函数似乎返回 2 个表格,但它只返回第一个参数传递的相同的 2 个表格。

例如,在 Lua 中,当我运行以下命令时:

local a, b = {3}, {4}
local c, d = process(a, b)
print(c[1], d[1])

我得到的结果是:

0.3 0.3

但是我希望得到:

0.3 0.4

我应该如何更改 SWIG typemap 使其按预期工作?

点赞
用户1944004
用户1944004

我无法使用以下最小示例重现您的问题。

test.i

%模块示例
%{
#include“test.h”
%}

%包括<typemaps.i>

%应用(float * INOUT,int){(float * io1,int n1)};
%应用(float * INOUT,int){(float * io2,int n2)};

%包括“test.h”

test.h

#pragma once

void process(float *io1,int n1,float *io2,int n2);

test.c

#include “test.h”

void process(float * io1,int n1,float * io2,int n2){
    forint i = 0; i <n1;++ i){
        io1 [i] = io1 [i] * 0.1;
        io2 [i] = io2 [i] * 0.1;
    }
}

test.lua

local example = require(“example”)
local a,b = {3},{4}
local c,d = example.process(a,b)
print(c [1],d [1])

然后我使用编译并运行

$ swig -lua test.i
$ cc - fPIC -shared -I /usr/include/lua5.3/ test_wrap.c test.c -o example.so
$ lua5.3 test.lua
0.30000001192093    0.40000000596046

第7个小数位后的垃圾值源于将float提升为默认为doublelua_Number

不考虑此问题,我看到了预期的0.3 0.4。这意味着错误必须在您未显示的某些代码中。请确保在解析process原型之前%apply typemaps,即在上面的示例中注意%apply%include“test.h”之前。

2019-08-06 01:56:37