1

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

Translunar
  • 3,739
  • 33
  • 55
  • 1
    Is ruby-dev package installed? – David Ranieri Jun 21 '13 at 22:34
  • It's not Ubuntu, so no. Unless there's some way to install ruby-dev of which I am unaware on Mac OS X. But even if so, that wouldn't make sense since my other Ruby C extensions compile just fine. – Translunar Jun 21 '13 at 23:57
  • looks like it is complaining about `ruby/subst.h` which it cannot `#include` from `ruby.h`. – akonsu Jun 22 '13 at 03:18
  • Why is it looking in two locations for headers? I see `/System/Library/Frameworks/ruby.framework/Headers/intern.h:409` and `/Users/jwoods/.rbenv/versions/2.0.0-p195/include/ruby-2.0.0/ruby/ruby.h:1718`. Maybe not relevant, but seems strange to me. – Translunar Jun 22 '13 at 22:58
  • you forgot object extension, should be: `$objs = %w{hypergeometric.o}` – NARKOZ Jun 18 '14 at 16:18
  • @NARKOZ Thanks, only a year too late. =P – Translunar Jun 18 '14 at 17:22

1 Answers1

0

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.

Translunar
  • 3,739
  • 33
  • 55