crubyextconf.rbmkmf

Simple Ruby C extension cannot find ruby.h


I wrote an incredibly simple C extension for Ruby. Unfortunately, it can't find ruby.h.

Here is the extconf.rb:

require "mkmf"
$srcs = %w{hypergeometric.c}
$objs = %w{hypergeometric}
create_makefile("hypergeometric")

And here is the only source file:

#include <ruby.h> // have also tried #include "ruby.h", same error.
VALUE cHypergeometric;
void Init_hypergeometric() {
  cHypergeometric = rb_define_module("Hypergeometric");
}

When I try to compile this, I get the following error:

../../../../ext/hypergeometric/hypergeometric.c:1:18: error: ruby.h: No such file or directory

Full gist. Note that line numbers are off because I've mostly commented out code instead of deleting it.

Interestingly, if I try to compile again without cleaning, I get a different error:

ld: warning: ignoring file hypergeometric, file was built for unsupported file format ( 0x67 0x70 0x63 0x68 0x43 0x30 0x31 0x33 0x38 0x4b 0x73 0x92 0x3d 0xf1 0xa5 0x62 ) which is not the architecture being linked (x86_64): hypergeometric

(The full output is again included in the gist above.)

Perhaps this is a clue?

Why can't it find ruby.h? When I compile NMatrix or the SciRuby rb-gsl fork, both of these work fine and have no problem locating ruby.h. (For the record, I wrote NMatrix, C extension and all -- and I based this particular extension on that one's extconf.rb.)


Solution

  • The answer was astoundingly simple.

    I had a line in my extconf.rb: $objs = %w{hypergeometric}, which I copied from a different project. It turns out I missed the second half of the line:

    $objs = %w{hypergeometric}.map { |i| i + ".o" }
    

    I cannot for the life of me understand why this change would allow it to suddenly find ruby.h.

    In the end, the give-away was that weird error message:

    ld: warning: ignoring file hypergeometric, file was built for unsupported file format ( 0x67 0x70 0x63 0x68 0x43 0x30 0x31 0x33 0x38 0x4b 0x73 0x92 0x3d 0xf1 0xa5 0x62 ) which is not the architecture being linked (x86_64): hypergeometric
    

    but odd that it only happens the second time I run bundle exec rake compile.