我为 Ruby 编写了一个非常简单的 C 扩展。不幸的是,它找不到 ruby.h
。
这是 extconf.rb
:
require "mkmf"
$srcs = %w{hypergeometric.c}
$objs = %w{hypergeometric}
create_makefile("hypergeometric")
这里是唯一的源文件:
#include <ruby.h> // have also tried #include "ruby.h", same error.
VALUE cHypergeometric;
void Init_hypergeometric() {
cHypergeometric = rb_define_module("Hypergeometric");
}
当我尝试编译它时,出现以下错误:
../../../../ext/hypergeometric/hypergeometric.c:1:18: error: ruby.h: No such file or directory
Full gist.请注意,行号已关闭,因为我主要注释掉代码而不是删除它。
有趣的是,如果我在不清理的情况下再次编译,我会得到一个不同的错误:
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
(完整的输出再次包含在上面的要点中。)
也许这是一个线索?
为什么找不到 ruby.h
?当我编译 NMatrix或 the SciRuby rb-gsl fork ,这两个都工作正常并且定位 ruby.h
没有问题。 (为了记录,我编写了 NMatrix、C 扩展和所有的 —— 我将这个特定的扩展基于那个的 extconf.rb
。)
最佳答案
答案非常简单。
我的 extconf.rb
中有一行:$objs = %w{hypergeometric}
,这是我从另一个项目复制的。原来我错过了这行的后半部分:
$objs = %w{hypergeometric}.map { |i| i + ".o" }
我这辈子都无法理解为什么这个改变会让它突然找到 ruby.h
。
最后,赠品是那个奇怪的错误消息:
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
但奇怪的是,它只在我运行bundle exec rake compile
时第二次发生。
关于c - 简单的 Ruby C 扩展找不到 ruby.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17244948/