0

I am trying to extend Ruby with a C extension in order to add an event hook.

Unfortunately, I get the following error:

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

The code I have written:

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

Can someone help me out solving this problem? Thanks in advance.

Paul Engel
  • 73
  • 1
  • 7

1 Answers1

1

EDIT: Try naming your rb_event_t something other than event. Even though I'm almost positive it's not a C keyword, the compile error you're getting seems to be consistent with that type of problem.

EDIT EDIT: Apparently not the actual problem, but it definitely looks like the error is on those two lines.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
  • You are right, Dennis. I have removed `self` as argument. I'm not experienced in writing Ruby C extensions. I am trying to strip down [ruby_prof.c](https://github.com/rdp/ruby-prof/blob/master/ext/ruby_prof/ruby_prof.c), but obviously I am doing something wrong. Also, `rb_event_t` is defined in [node.h](http://rxr.whitequark.org/mri/source/node.h) but I still get the same error. – Paul Engel Aug 03 '12 at 23:39
  • To be honest, I have zero experience with Ruby C extensions; I'm just giving advice based on the text of the compile error. – Dennis Meng Aug 03 '12 at 23:40
  • Unfortunately, changing `event` to `e` doesn't help. – Paul Engel Aug 03 '12 at 23:44
  • What about the compile errors when you switch `event` to `e`? – Dennis Meng Aug 03 '12 at 23:52
  • I have solved the problem. It seems that it is environment dependent `#if defined(RB_EVENT_HOOKS_HAVE_CALLBACK_DATA) || defined(RUBY_EVENT_VM) static void timber_trap(rb_event_flag_t event, VALUE data, VALUE self, ID id, VALUE klass); #else static void timber_trap(rb_event_t event, NODE *node, VALUE self, ID id, VALUE klass); #endif`. – Paul Engel Aug 04 '12 at 00:00
  • Glad you got it. Fwiw, you might be interested in `#ifdef`. – Dennis Meng Aug 04 '12 at 00:03