【问题标题】:Swig[C->Python]: How to handle input or output function arguments which are primitive types?Swig[C->Python]:如何处理原始类型的输入或输出函数参数?
【发布时间】:2013-03-01 01:24:51
【问题描述】:

例如,我有

my_types.h

typedef uint16_t my_type_1;
typedef uint8_t my_type_2;

my_types.c

int my_func(my_type_1 * arg1, my_type_2 * arg2, my_type_3 *arg3)
{
    *args3 = *arg1 + *arg2;
    return 0;
}

如何定义接口文件以将其包装到 python 中?

【问题讨论】:

  • 您的问题中有一些拼写错误 - 没有 my_type_3 typedef 并且您在 my_func 的定义中有 args3 而不是 arg3。

标签: python c swig


【解决方案1】:

假设您没有在 my_types.h 中也有 my_func 的声明,那么您的界面可以变成:

%module test

%{
#include "my_types.h"
%}

%include <stdint.i>

%include "my_types.h"

%apply uint16_t *INPUT { my_type_1 * arg1 };
%apply uint8_t *INPUT { my_type_2 * arg2 };
%apply uint16_t *OUTPUT { my_type_1 *arg3 };

int my_func(my_type_1 * arg1, my_type_2 * arg2, my_type_1 *arg3);

这足以让您按如下方式使用它:

import test

print test.my_func(100, 50)[1]

您的“真实”返回是元组中的位置 0,第一个 OUTPUT 位于位置 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多