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);

しかし、私はいつも同じエラーを受け取ります。このエラーはコンパイルを停止しませんが、起動するとバイナリsegfaultが発生しますが、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 に答える