【问题标题】:Swig simple type typedef argumentSwig 简单类型 typedef 参数
【发布时间】:2019-06-25 11:25:08
【问题描述】:

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

namespace numbers {
    typedef float float4;
}

我还创建了函数

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

Lua 代码

result = foo()
foo1( result )

错误:

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

swig 似乎可以识别 numbers::float4 但是在创建 lua 变量时它会以某种方式混淆。

【问题讨论】:

    标签: c++ lua swig


    【解决方案1】:

    评论太长了:

    我无法重现问题中的问题。您可以尝试以下步骤,看看是否仍然出现错误?

    我有以下 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
    

    【讨论】:

    • 抱歉,这是我的疏忽。我提供的示例只是一个示例。我的项目中有几个 .i 文件,但我忘记在其中一个文件中添加包含,将此归咎于其他 .i 文件。谢谢你的时间,我真的很感激。
    • @Mahashi 很高兴能帮上忙。这就是为什么始终创建Minimal, Complete, and Verifiable example 很重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多