c - 简单的 Ruby C 扩展找不到 ruby​​.h

标签 c ruby extconf.rb mkmf

我为 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?当我编译 NMatrixthe 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/

相关文章:

javascript - 如何使用 Objective C 或 javascript 在 cydget 中运行系统命令?

ruby native 扩展 : undefined symbol

c - Linux终端中的shell程序

ruby - 如何更新哈希计数

c - 为什么全宽移位对于常量和变量的表现不同?

javascript - 没有验证的属性上的客户端验证成功消息?

ruby - 打包编译好的二进制文件/带 Ruby Gem

c - C 中的中断错误

c - 内核中的 Futex 和阻塞