我正在尝试使用 C 扩展来扩展 Ruby,以添加事件挂钩。
不幸的是,我收到以下错误:
timber.c:7: error: expected ‘)’ before ‘event’
timber.c: In function ‘timber_init_event_hook’:
timber.c:15: error: ‘timber_trap’ undeclared (first use in this function)
timber.c:15: error: (Each undeclared identifier is reported only once
timber.c:15: error: for each function it appears in.)
timber.c: At top level:
timber.c:21: error: expected ‘)’ before ‘event’
make: *** [timber.o] Error 1
我写的代码:
#include <ruby.h>
#include </Users/paulengel/.rvm/src/ruby-1.9.2-p180/node.h> // #include <node.h> raises `error: node.h: No such file or directory`
// Declarations
static void timber_init_event_hook();
static void timber_trap(rb_event_t event, NODE *node, VALUE self, ID mid, VALUE klass);
VALUE timber_start(VALUE self);
void Init_timber();
// Definitions
static void timber_init_event_hook() {
#if defined(RB_EVENT_HOOKS_HAVE_CALLBACK_DATA) || defined(RUBY_EVENT_VM)
rb_add_event_hook(timber_trap, RUBY_EVENT_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN, 0);
#else
rb_add_event_hook(timber_trap, RUBY_EVENT_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN);
#endif
}
static void timber_trap(rb_event_t event, NODE *node, VALUE self, ID mid, VALUE klass) {
rb_funcall(rb_mKernel, rb_intern("puts"), 1, rb_str_new2(event));
}
VALUE timber_start(VALUE self) {
// Do something
}
void Init_timber() {
VALUE mTimber = rb_define_module("Timber");
timber_init_event_hook();
rb_define_singleton_method(mTimber, "start", timber_start, 0);
}
有人可以帮我解决这个问题吗?提前致谢。