2

我想将 rb_require 与 rb_protect 一起使用,如下例所示:

int error;
rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error);

但是当我编译它时,我得到了这个错误:

passing argument 1 of ‘rb_protect’ from incompatible pointer type [enabled by default]
/usr/include/ruby-1.9.1/ruby/intern.h:357:7: note: expected ‘VALUE (*)(VALUE)’ but argument is of type ‘VALUE (*)(VALUE,  VALUE)’

在 Google 上搜索以查看如何将 rb_require 与 rb_protect 一起使用后,我尝试了:

int error;
rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./test", &error);

或者

VALUE require_wrap(VALUE arg)
{
return rb_require("./test");
}
/*in main:*/
rb_protect( require_wrap, 0, & error);

但我总是得到同样的错误。当我启动它时,此错误不会停止编译,而是二进制段错误,而在没有 rb_protect 的情况下一切正常。

__编辑__

我的源文件中有错误。事实上,我测试过的所有解决方案都运行良好:

int error;
rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error);

或者

int error;
rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./test", &error);

或者

VALUE require_wrap(VALUE arg)
{
  return rb_require("./test");
}
/*in main:*/
rb_protect( require_wrap, 0, & error);

谢谢

4

1 回答 1

0

行之有效的解决方案:

int error;
rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "./test", &error);

或者

int error;
rb_protect( RUBY_METHOD_FUNC(rb_require), (VALUE) "./test", &error);

或者

VALUE require_wrap(VALUE arg)
{
  return rb_require("./test");
}
/*in main:*/
rb_protect( require_wrap, 0, & error);
于 2012-04-06T08:11:34.250 回答