Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top