Swig 简单类型 typedef 参数

Swig simple type typedef argument

本文关键字:参数 typedef 类型 简单 Swig      更新时间:2023-10-16

我为swig到lua中的简单类型创建了C++typedef,类似于

namespace numbers {
    typedef float float4;
}

我还创建了函数

numbers::float4 foo();
void foo1( numbers::float4 );

路易代码

result = foo()
foo1( result )

错误:

 Lua error: Wrong arguments for overloaded function                                                                                                                                                                                           
   Possible C/C++ prototypes are:                                                                                                                                                                                                                                               
     foo1( numbers::float4 ) 

SWIG似乎可以识别数字::float4,但是在创建Lua变量时,它不知何故会感到困惑。

评论太长了:

我无法在问题中重现该问题。 您可以尝试以下步骤,看看是否仍然收到错误吗?

我有以下C++文件:

test.hpp

#pragma once
namespace numbers {
    typedef float float4;
}
numbers::float4 foo();
void foo1( numbers::float4 );

test.cpp

#include <iostream>
#include "test.hpp"
numbers::float4 foo() {
    return 3.14f;
}
void foo1(numbers::float4 f) {
    std::cout << f << 'n';
}

还有懒人的SWIG接口文件:

test.i

%module example
%{
#include "test.hpp"
%}
%include "test.hpp"

然后我生成包装器代码并将所有内容编译为共享对象。

$ swig -c++ -lua test.i
$ g++ -I/usr/include/lua5.2 -fPIC -shared test_wrap.cxx test.cpp -o example.so

以下示例脚本运行良好。

test.lua

local example = require"example"
local foo = example.foo
local foo1 = example.foo1
result = foo()
foo1( result )
$ lua test.lua
3.14