Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为C+创建ruby扩展+;C语言库 我试图为C++库构建Ruby扩展。首先,我把C++的代码打包成C。我使用Eclipse IDE构建它,它工作得很好。然后我尝试为该C源代码构建ruby扩展。这是我试过的代码_C++_C_Ruby_Word Wrap - Fatal编程技术网

为C+创建ruby扩展+;C语言库 我试图为C++库构建Ruby扩展。首先,我把C++的代码打包成C。我使用Eclipse IDE构建它,它工作得很好。然后我尝试为该C源代码构建ruby扩展。这是我试过的代码

为C+创建ruby扩展+;C语言库 我试图为C++库构建Ruby扩展。首先,我把C++的代码打包成C。我使用Eclipse IDE构建它,它工作得很好。然后我尝试为该C源代码构建ruby扩展。这是我试过的代码,c++,c,ruby,word-wrap,C++,C,Ruby,Word Wrap,/* *圈.h * *创建日期:2013年9月18日 *作者:科勒姆 */ /* *圆形w.h * *创建日期:2013年9月18日 *作者:科勒姆 */ /* *圆圈w.cpp * *创建日期:2013年9月18日 *作者:科勒姆 */ 主语 #include "Circle_w.h" #include <stdio.h> #include <stdlib.h> int main() { float radius = 1.5; // Get a po

/* *圈.h * *创建日期:2013年9月18日 *作者:科勒姆 */

/* *圆形w.h * *创建日期:2013年9月18日 *作者:科勒姆 */

/* *圆圈w.cpp * *创建日期:2013年9月18日 *作者:科勒姆 */

主语

#include "Circle_w.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    float radius = 1.5;

    // Get a pointer to a Circle object
    void *circle = Circle_C_new(radius);

    // Pass the Circle object to the wrapper methods
    float area = Circle_C_getArea(circle);

    printf ("Circle of radius %f has area %f\n", radius, area);

    // Free the Circle object memory
    void *pointer=Circle_C_delete(circle);

    return 0;
}
这是我用来测试模块的ruby类

test.rb文件

require '/home/kelum/workspace/Test/TestLemon'
include TestLemon
puts test
问题是我怎样才能一起编译呢?当我只运行extconf.rb和test.rb时,它会显示错误

 TestLemon.so: undefined symbol: Circle_C_new

我认为是因为ExtFuff.RB不执行包装C++的构建。我怎样才能做到这一点?


非常感谢。

这里的库是否代表一个单独安装的动态链接库,或者是用C++编写的源代码?如果是前者,您需要将其作为库提供,并告诉mkmf使用它。如果是后者,那么你需要在EXT文件夹中有.cpp文件的拷贝——它在哪里?BTW,你有一个特定的要求在C中包装而不是C++,可以在C++中制作红宝石绑定。看看米饭:我也有过这样的问题:谢谢你的回答。我尝试使用C++写的源头文件。在这个例子中,循环是用C++编写的源代码。我只是在同一个文件夹中使用它。我无法使用extconf.rb编译包装以使test.o可用于TestLemon.so。你好,尼尔,我看了你的ruby_nex_cpp代码。我不理解baz_vector_ruby.cpp和baz_vector_ruby.h的用法。你能解释一下吗?我试着用一种简单的方法来做这件事。它显示出一些错误。我把它贴在这里了。。这个实现有什么问题吗?我试着回答你的另一个问题。你能不能更准确地解释一下我的文件中有什么让你困惑,我不知道该解释什么?但是基本上我的原型GEM展示了一种包装C++类的方法,这样它就可以在Ruby中访问,就像它是Ruby类一样。如果你想经常这样做,我建议你看看RICE——我的代码很有效,但只是一个关于“如何做”的快速研究,而RICE是一个具有许多经过良好测试的特性的框架。
#include "Circle_w.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    float radius = 1.5;

    // Get a pointer to a Circle object
    void *circle = Circle_C_new(radius);

    // Pass the Circle object to the wrapper methods
    float area = Circle_C_getArea(circle);

    printf ("Circle of radius %f has area %f\n", radius, area);

    // Free the Circle object memory
    void *pointer=Circle_C_delete(circle);

    return 0;
}
#include "ruby.h"
#include "Circle_w.h"
#include <stdio.h>

extern VALUE method_test(){
float r=1.5;
void *circle = Circle_C_new(r);
int area = (int)Circle_C_getArea(circle);
printf("area %d \n",area);
return INT2NUM(area);
}

extern void Init_TestLemon() {
VALUE lemon = rb_define_module("TestLemon");
rb_define_method(lemon, "test", method_test, 0);
}
require 'mkmf'
extension_name = 'TestLemon'
dir_config(extension_name)
create_makefile(extension_name)
require '/home/kelum/workspace/Test/TestLemon'
include TestLemon
puts test
 TestLemon.so: undefined symbol: Circle_C_new