我正在尝试为使用结构的 C 代码编写 Python 包装。
modules.c:
struct foo
{
int a;
};
struct foo bar;
模数.i
%module nepal
%{
struct foo
{
int a;
}
%}
extern struct foo bar;
但是在编译过程中出现错误:
在函数“Swig_var_bar_set”中: 错误:‘bar’未声明(首次在此函数中使用)
您能帮我如何正确定义导出结构变量吗?
最佳答案
试试这个:
%module nepal
%{
struct foo
{
int a;
};
extern struct foo bar;
%}
struct foo
{
int a;
};
extern struct foo bar;
%{ %} 中的代码被插入到包装器中,它下面的代码被解析以创建包装器。将这些全部放在头文件中会更容易,这样就不会那么重复:
modules.h
struct foo
{
int a;
};
extern struct foo bar;
modules.c
#include "modules.h"
struct foo bar;
modules.i
%module nepal
%{
#include "modules.h"
%}
%include "modules.h"
关于python - Swig - 包装 C 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2676453/