【问题标题】:Accessing a C struct from RubyMotion从 RubyMotion 访问 C 结构体
【发布时间】:2014-12-02 21:15:07
【问题描述】:

我正在尝试在 RubyMotion 中使用 giflib C 库。它在供应商文件夹中编译得很好。但我仍然不确定 Ruby 中的 C 结构访问:

error = Pointer.new(:int)
pointer = DGifOpenFileName("foobar.gif", error) # returns (GifFileType *)

C 结构:

typedef struct GifFileType {
    GifWord SWidth, SHeight;
    // ...
    ColorMapObject *SColorMap;
    // ...
} GifFileType;

访问 Ruby 指针会产生运行时错误:

puts pointer.value # => Can't find pointer description for type `{ColorMapObject}'

我做错了什么?

(这里是完整的gif_lib.h 和生成的giflib.bridgesupport。)

【问题讨论】:

    标签: c ruby pointers struct rubymotion


    【解决方案1】:

    您正在做的似乎应该有效。我会向motion support 提交报告。

    RubyMotion Runtime Guide 表示以下内容:

    C 结构映射到 RubyMotion 中的类。结构可以在 Ruby 中创建并传递给需要 C 结构的 API。同样,返回 C 结构的 API 将返回相应结构类的实例。 结构类对它所包装的 C 结构的每个字段都有一个访问器方法。 例如,以下代码创建一个 CGPoint 结构,设置其 x 和 y 字段,然后将其传递给 drawAtPoint:withFont: 方法。

    pt = CGPoint.new
    pt.x = 100
    pt.y = 200
    'Hello'.drawAtPoint(pt, withFont: font)
    

    可以将字段值直接传递给构造函数。

    pt = CGPoint.new(100, 200)
    'Hello'.drawAtPoint(pt, withFont: font)
    

    RubyMotion 也将接受数组作为便利。它们必须包含结构中预期的相同数量和类型的对象。

    'Hello'.drawAtPoint([100, 200], withFont: font)
    

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 1970-01-01
      • 2021-05-21
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      相关资源
      最近更新 更多