开发者

Swig - wrapping C struct

开发者 https://www.devze.com 2022-12-27 18:25 出处:网络
I am trying to write Python wrap for C code which uses struct. modules.c: struct foo { int a; }; struct foo bar;

I am trying to write Python wrap for C code which uses struct.

modules.c:

struct foo
{
    int a;
};

struct foo bar;

modulues.i

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

extern struct foo bar;

But during compiling I am given error:

In f开发者_如何学Gounction ‘Swig_var_bar_set’: error: ‘bar’ undeclared (first use in this function)

Could you be so kind to help me how to correctly define export struct variable ?


Try this:

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

    extern struct foo bar;
%}

struct foo
{
    int a;
};

extern struct foo bar;

The code in %{ %} is inserted in the wrapper, and the code below it is parsed to create the wrapper. It's easier to put this all in a header file so it is not so repetitive:

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

精彩评论

暂无评论...
验证码 换一张
取 消