2010-04-20 120 views
2

我想为使用struct的C代码编写Python包装。Swig - 包装C struct

modules.c:

struct foo 
{ 
    int a; 
}; 

struct foo bar; 

modulues.i

%module nepal 
%{ 
    struct foo 
    { 
     int a; 
    } 
%} 

extern struct foo bar; 

但是在编译期间,我给出的错误:

在功能 'Swig_var_bar_set': 错误:“栏'未申报(首次使用此功能)

你能帮助我如何正确定义导出结构变量吗?

+1

您是否考虑过使用'ctypes'模块代替SWIG?这很容易。 – 2010-04-20 16:18:22

回答

2

尝试这种情况:

%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"