【发布时间】:2018-06-07 12:05:17
【问题描述】:
我有一个指向 C 结构的指针,我需要知道如何在使用 swig 生成 python 库后从 python 初始化结构。
我能够编译代码并运行 swig 没有任何错误,我还能够将库导入 python。
我的目标是学习如何在 c/swig/python 中使用指针。
这是我要集成到python中的c函数:
#include <stdio.h>
#include "myswig.h"
myData* myhfun(myData *data){
data->s = data->r1 + data->r2;
printf("sum(%f+%f)=%f\n", data->r1, data->r2, data->s);
return data;
}
这是相同的头文件:
//file myswig.h
struct myData{
double r1;
double r2;
double s;
};
typedef struct myData myData;
myData* myhfun(myData *data);
我的 swig 接口文件是这样的
/* file: myswig.i */
%module myswig
%{
#include "myswig.h"
%}
/*my function */
myData* myhfun(myData *data);
运行 swig 和测试的脚本:
swig -python -Isrc myswig.i
gcc -Isrc -fPIC -I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m -c src/myswig.c myswig_wrap.c
gcc -shared -fPIC -o _myswig.so myswig.o myswig_wrap.o
python3 -c "import myswig"
【问题讨论】:
标签: c python-3.x swig swig-template