【问题标题】:Swig - wrapping C structSwig - 包装 C 结构
【发布时间】:2011-02-10 05:05:18
【问题描述】:

我正在尝试为使用 struct 的 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”未声明(在此函数中首次使用)

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

【问题讨论】:

  • 您是否考虑过使用 ctypes 模块而不是 SWIG?这要容易得多。

标签: python c struct swig


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多