用 SWIG 包装 C 结构 [英] Wrapping C structs with SWIG

查看:48
本文介绍了用 SWIG 包装 C 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含以下类型定义的 C 头文件:

I have C header file containing the following type definition:

// example.h
typedef struct Vertex {
  int color;
} Vertex;

我尝试用 SWIG 包装这个结构,但显然我做错了什么.我的 SWIG 接口文件看起来像

I try to wrap this struct with SWIG, but apparently I am doing something wrong. My SWIG interface file looks like

// example.i
%module example
%inline %{
#include "example.h"
}

但是如果我将头文件的内容复制到我的接口文件中,使后者看起来像

But if I copy the contents of my header file into my interface file so that the latter looks like

%module example

%inline %{
typedef struct Vertex {
  int color;
} Vertex;
%}

我可以通过以下方式从 Ruby 访问结构

I can access the struct from Ruby in the following way

irb> require 'example'
# => true
irb> Examlpe::Vertex
# => Vertex

有没有办法自动包装头文件?不想每次修改头文件的内容都复制粘贴到接口文件中.

Is there a way to automatically wrap a header file? I don't want to copy and paste the contents of the header file to the interface file every time I change it.

预先感谢您的帮助.

-- t6d

推荐答案

自从我使用 Swig 已经有一段时间了,但我记得 %inline 用于将内联部分直接传递给编译器;Swig 本身没有看到它,我认为您需要的是:

It's been a while since I used Swig but as I recall %inline is used to pass through the inline part directly to the compiler; Swig itself doesn't see it, What I think you need is:

%module example
%include<example.h>

这篇关于用 SWIG 包装 C 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆