- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
当我运行命令 rake compile
时,我很难弄清楚为什么我的 rb_define_singleton 和 rb_define_method 调用会中断。
首先,一个问题可能是我实际上不确定最后一个整数参数实际代表什么,以便决定使用什么值,而且我无法找到解释这个的文档,但我觉得我是只是猜测,但问题似乎出在第三个参数上,所以我担心的是
其次,我的构建失败并在调用 make
时出现以下错误:
make compiling ../../../../ext/fftw/fftw.cpp ../../../../ext/fftw/fftw.cpp: In function 'void Init_fftw()': ../../../../ext/fftw/fftw.cpp:64:58: error: invalid conversion from 'VALUE ()(VALUE, VALUE) {aka long unsigned int ()(long unsigned int, long unsigned int)}' to 'VALUE ()(...) {aka long unsigned int ()(...)}' [-fpermissive] rb_define_singleton_method(cNMatrix, "r2c", fftw_r2c, 1); ^ In file included from /Users/private/.rvm/rubies/ruby-2.1.2/include/ruby-2.1.0/ruby/ruby.h:1694:0, from /Users/private/.rvm/rubies/ruby-2.1.2/include/ruby-2.1.0/ruby.h:33, from ../../../../ext/fftw/fftw.cpp:1: /Users/private/.rvm/rubies/ruby-2.1.2/include/ruby-2.1.0/ruby/intern.h:216:6: error: initializing argument 3 of 'void rb_define_singleton_method(VALUE, const char*, VALUE ()(...), int)' [-fpermissive] void rb_define_singleton_method(VALUE, const char, VALUE(*)(ANYARGS), int); ^ make: * [fftw.o] Error 1 rake aborted!
这个错误意味着我根本无法测试 ruby 中的函数,因为我需要使这个定义成功,所以我首先将文件剥离到最低限度来尝试这样做
到目前为止,我尝试了多种类型的转换,但似乎都不起作用。我开始怀疑我在 extconf 中使用我的标志和编译器选项设置错误,但我不确定。
source code for the failing file is on github
欢迎任何建议!谢谢
最佳答案
ruby C api 的文档有点缺乏。关于此的唯一“官方”文档是 extension.rdoc
您的 C 函数可以具有 20 个签名之一,但是由于 C 中没有重载,并且创建所有 rb_define_method
变体的 20 个版本会给您使用的接口(interface)增加很多重复 RUBY_METHOD_FUNC(your_function)
对您的函数指针进行类型转换。
整数参数告诉 ruby 你正在使用哪个签名:
0 到 17 表示您的 C 函数如下所示:
VALUE some_function(VALUE self, VALUE arg1, VALUE arg2); /* if you had passed 2 */
整数只是函数接受的参数个数,不包括 self(因此这也与 ruby land 中传递的参数个数相同)
-1 意味着你的函数应该有签名
VALUE some_function(VALUE self, VALUE *argv, VALUE argc);
即您将获得一组参数和参数数量。这也意味着 ruby 将允许使用任意数量的参数调用该方法(即报告的 arity 将为 -1)。 rb_scan_args
在这种情况下通常很有用。
最后,-2 表示您的函数看起来像
VALUE some_function(VALUE self, VALUE args);
和 args
是一个包含您的参数的 ruby 数组(这同样允许调用者传递任意数量的参数)。
不幸的是,类型转换意味着如果你弄错了,你的代码将在运行时以某种未定义的方式崩溃,而不是编译时错误。
关于c++ - 在 C(或 C++)中使用 rb_define_singleton_method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24829572/
当我运行命令 rake compile 时,我很难弄清楚为什么我的 rb_define_singleton 和 rb_define_method 调用会中断。 首先,一个问题可能是我实际上不确定最后一
我是一名优秀的程序员,十分优秀!