c - 如何在 C -> ruby​​ 接口(interface)中返回变量?

标签 c ruby interface ffi rubydl

对先前问题的跟进,显示当我尝试从目标库获取错误消息时失败的部分:

require 'gt4r'

@@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini"
@@normal_user = "BMCHARGUE"

describe Gt4r do
  it 'initializes' do
      rv = Gt4r.gTD_initialize @@normal_user, @@normal_user, @@test_environment
      Gt4r.gTD_get_error_message rv, @msg
      @msg.should == ""
      rv.should == 0
  end
end

我希望在 @msg 中返回错误消息,但运行时我得到以下信息:

Gt4r
(eval):5: [BUG] Segmentation fault
ruby 1.8.6 (2008-08-11) [i386-mswin32]


This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

如果我使用符号 (:msg) 来代替:

C:\code\GraphTalk\gt4r_dl>spec -fs -rgt4r gt4r_spec.rb

Gt4r
- initializes (ERROR - 1)

1)
NoMethodError in 'Gt4r initializes'
undefined method `to_ptr' for :msg:Symbol
(eval):5:in `call'
(eval):5:in `gTD_get_error_message'
./gt4r_spec.rb:9:

Finished in 0.046 seconds

1 example, 1 failure

显然我遗漏了有关在 ruby​​ 和 C 之间传递参数的一些内容。我需要什么样的 ruby​​ 变量才能返回我的值?

最佳答案

哪个位失败了?我猜是这样的:

Gt4r.gTD_get_error_message rv, @msg

该方法是如何实现的?该方法的 C 签名是什么? (*字符)?

顺便说一句:冒着听起来像是破纪录的风险,编写 C 扩展比尝试将内容硬塞到 DL 中要容易得多。 AFAIK,除了映射单一功能的一次性黑客之外,没有人使用深度学习。你提到FFI在Windows下不起作用,你可能想看看RubyInline( http://www.zenspider.com/ZSS/Products/RubyInline/ ),或者只是使用普通的C扩展(如果你足够了解C来使用DL,那么你也足够了解C来编写扩展)

关于c - 如何在 C -> ruby​​ 接口(interface)中返回变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/301504/

相关文章:

c - textString 之前的预期表达式

ruby - Jekyll 构建失败 : Liquid Exception: cannot load such file -- yajl

ruby - 使用 mechanize 时使用正则表达式查找具有特定 anchor 的链接

ruby - 将 block 作为方法参数传递在 Ruby 1.9.3 中即将消亡

algorithm - golang []interface{} 不能作为函数参数吗?

c - 将每个成员初始化为一个空的 C 字符串

c - 如何获取文本文件c中最常出现的字符串

types - 无开销的开关接口(interface)实现

c - 在 C 项目中使用 OpenGL 4.3

java - 我可以在不同的接口(interface)中为不同的方法使用相同的名称吗?