Tag: ruby c extension

如何编译/创建使用c的ruby扩展?

我想创建一个使用c的ruby扩展。 但是当我使用gcc编译它时,我收到此错误: gcc rubyext.c -orubyext -I /usr/local/include/ruby-1.9.1/ In file included from rubyext.c:1: /usr/local/include/ruby-1.9.1/ruby/ruby.h:25:25: error: ruby/config.h: No such file or directory In file included from rubyext.c:1: /usr/local/include/ruby-1.9.1/ruby/ruby.h:107: error: ‘SIZEOF_INT’ undeclared here (not in a function) /usr/local/include/ruby-1.9.1/ruby/ruby.h:108: error: ‘SIZEOF_LONG’ undeclared here (not in a function) /usr/local/include/ruby-1.9.1/ruby/ruby.h:112: error: ‘SIZEOF_VOIDP’ undeclared here (not in a function) In file included […]

将ruby数组值传递到C数组中

我正在尝试基于此配方为C中的ruby创建一个独立的FFT扩展 我已经注意到几种在ruby和c之间传递不同值的方法。 然而,对于ruby和C来说,它都是相当新的,并且无法解决如何将数组从VALUE ruby​​对象复制到C数组中的问题。 编译错误:SimpleFFT.c:47:错误:下标值既不是数组也不是指针 和代码: #include “ruby.h” #include “fft.c” // the c file I wish to wrap in ruby VALUE SimpleFFT = Qnil; void Init_simplefft(); VALUE method_rfft(VALUE self, VALUE anObject); void Init_simplefft() { SimpleFFT = rb_define_module(“SimpleFFT”); rb_define_method(SimpleFFT, “rfft”, method_rfft, 1); } VALUE method_rfft(VALUE self, VALUE inputArr) { int N = RARRAY_LEN(inputArr); // this works […]

Ruby C扩展API问题

所以,最近我不幸地需要为Ruby做一个C扩展(因为性能)。 由于我在理解VALUE时遇到了问题(现在仍然如此),所以我查看了Ruby源代码并发现: typedef unsigned long VALUE; ( 链接到Source ,但你会注意到还有一些其他’方法’已经完成,但我认为它基本上是一个long ;如果我错了,请纠正我)。 因此,在进一步调查时,我发现了一篇有趣的博文 ,其中说: “……在某些情况下,VALUE对象可能是数据,而不是指向数据。” 令我困惑的是,当我尝试从Ruby传递字符串到C时,并使用RSTRING_PTR(); 在VALUE (从Ruby传递给C函数),并尝试使用strlen();进行’调试’ strlen(); 它返回4. 总是 4。 示例代码: VALUE test(VALUE inp) { unsigned char* c = RSTRING_PTR(inp); //return rb_str_new2(c); //this returns some random gibberish return INT2FIX(strlen(c)); } 此示例始终返回1作为字符串长度: VALUE test(VALUE inp) { unsigned char* c = (unsigned char*) inp; //return rb_str_new2(c); // […]

mkmf在编译C扩展时忽略子文件夹中的文件

我想像这样组织C源代码: + / | |___ + ext | | | |___ + native_extension | | | |___ + lib | | | | | |___ (Source files are kept in here – may contain sub-folders) | | | |___ native_extension.c | |___ native_extension.h | |___ extconf.rb | |___ + lib | | | |___ (Ruby […]