Swig - 包装 C 结构

发布于 2024-08-29 17:44:38 字数 382 浏览 1 评论 0原文

我正在尝试为使用 struct 的 C 代码编写 Python 包装。

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”中: 错误:“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 function ‘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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-09-05 17:44:38

试试这个:

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

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文