Swig-wrapping C struct

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

modules.c:

struct foo
{
    int a;
};

struct foo bar;

      

modulues.i

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

extern struct foo bar;

      

But at compile time I get the error:

In function 'Swig_var_bar_set: error: "undelared" (use in this function first)

Could you be kind enough to help me define the struct export variable correctly?

+2


a source to share


1 answer


Try the following:

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

    extern struct foo bar;
%}

struct foo
{
    int a;
};

extern struct foo bar;

      

The code in% {%} is inserted into the wrapper, and the code underneath is parsed to create the wrapper. It's easier to put all this in a header file so it doesn't repeat itself:

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"

      

+2


a source







All Articles