I am an experienced C/C++ developer but I am a novice in Ruby.
我是一个有经验的C/ c++开发人员,但是我是Ruby的新手。
How can I call a C++ function from with in Ruby?
如何在Ruby中调用c++函数?
4 个解决方案
#1
32
You have 3 possibilities :
你有三种可能:
1) Ruby is able to load libraries. Even if it is a bit tricky, you can decide to write your own loader and bind your C++ library in Ruby. This is done using what is called an extension module. You will find a comprehensive tutorial here: http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
1)Ruby能够加载库。即使有点棘手,您也可以决定编写自己的加载程序并在Ruby中绑定c++库。这是使用所谓的扩展模块完成的。您将在这里找到一个全面的教程:http://www.rubyinside.com/how- create-a-ruby-extension-in-c-in-under 5 minutes-100.html
2) You can use a tool that will generate the Ruby wrapper around your C++ library. Look at SWIG for example (http://www.swig.org/). You just have to create a file in a swig-specific syntax and provide it to SWIG. It will then be able to generate the wrapper for many languages, Ruby included.
2)您可以使用一个工具来生成围绕c++库的Ruby包装器。查看SWIG (http://www.swig.org/)。您只需以特定于swig的语法创建一个文件,并将其提供给SWIG。然后,它将能够为包括Ruby在内的许多语言生成包装器。
3) You can choose to use a middleware, such as CORBA/ICE/whatever. It may be a bit overkill if you only want to call some C++ functions, but it will allow you to remote call the functions, or "hide" a grid behind the middleware.
3)您可以选择使用中间件,例如CORBA/ICE/随便什么。如果您只想调用一些c++函数,那么它可能会有点过了,但是它将允许您远程调用函数,或者“隐藏”中间件背后的网格。
#2
13
To call C++ code from Ruby, you will likely want to build an extension.
要从Ruby调用c++代码,您可能需要构建一个扩展。
If you are an experienced C++ developer, you may feel comfortable with Rice:
如果您是一个有经验的c++开发人员,您可能会对Rice感到满意:
https://github.com/jasonroelofs/rice
https://github.com/jasonroelofs/rice
It uses C++ metaprogramming techniques to simplify writing extensions.
它使用c++元编程技术来简化编写扩展。
If you were calling into C, you could also use ffi. Calling C++ code is a little more complicated than calling C code due to name mangling and exceptions.
如果您正在调用C,您也可以使用ffi。由于名称管理和异常,调用c++代码要比调用C代码复杂一些。
#3
5
I believe the questioner is asking how to call C++ from with in Ruby, if so then the for simple C/C++ RubyInline1 is by the far the simplest solution.
我相信提问者在问如何从Ruby中调用c++,如果是这样的话,那么简单的C/ c++ RubyInline1是目前最简单的解决方案。
Alternatively if you need to call more substatntial C++ code, you can build a ruby extension. Here is a good tutorial
或者,如果您需要调用更多的子状态c++代码,您可以构建一个ruby扩展。这里有一个很好的教程。
#4
2
You need to wrap your c++ code in a C interface and then bind those C functions to ruby methods using rb_define_method()
您需要将c++代码封装到c接口中,然后使用rb_define_method()将这些c函数绑定到ruby方法中
alternatively you can use SWIG, as Aurelien said.
或者你也可以用SWIG,正如Aurelien所说。
#1
32
You have 3 possibilities :
你有三种可能:
1) Ruby is able to load libraries. Even if it is a bit tricky, you can decide to write your own loader and bind your C++ library in Ruby. This is done using what is called an extension module. You will find a comprehensive tutorial here: http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
1)Ruby能够加载库。即使有点棘手,您也可以决定编写自己的加载程序并在Ruby中绑定c++库。这是使用所谓的扩展模块完成的。您将在这里找到一个全面的教程:http://www.rubyinside.com/how- create-a-ruby-extension-in-c-in-under 5 minutes-100.html
2) You can use a tool that will generate the Ruby wrapper around your C++ library. Look at SWIG for example (http://www.swig.org/). You just have to create a file in a swig-specific syntax and provide it to SWIG. It will then be able to generate the wrapper for many languages, Ruby included.
2)您可以使用一个工具来生成围绕c++库的Ruby包装器。查看SWIG (http://www.swig.org/)。您只需以特定于swig的语法创建一个文件,并将其提供给SWIG。然后,它将能够为包括Ruby在内的许多语言生成包装器。
3) You can choose to use a middleware, such as CORBA/ICE/whatever. It may be a bit overkill if you only want to call some C++ functions, but it will allow you to remote call the functions, or "hide" a grid behind the middleware.
3)您可以选择使用中间件,例如CORBA/ICE/随便什么。如果您只想调用一些c++函数,那么它可能会有点过了,但是它将允许您远程调用函数,或者“隐藏”中间件背后的网格。
#2
13
To call C++ code from Ruby, you will likely want to build an extension.
要从Ruby调用c++代码,您可能需要构建一个扩展。
If you are an experienced C++ developer, you may feel comfortable with Rice:
如果您是一个有经验的c++开发人员,您可能会对Rice感到满意:
https://github.com/jasonroelofs/rice
https://github.com/jasonroelofs/rice
It uses C++ metaprogramming techniques to simplify writing extensions.
它使用c++元编程技术来简化编写扩展。
If you were calling into C, you could also use ffi. Calling C++ code is a little more complicated than calling C code due to name mangling and exceptions.
如果您正在调用C,您也可以使用ffi。由于名称管理和异常,调用c++代码要比调用C代码复杂一些。
#3
5
I believe the questioner is asking how to call C++ from with in Ruby, if so then the for simple C/C++ RubyInline1 is by the far the simplest solution.
我相信提问者在问如何从Ruby中调用c++,如果是这样的话,那么简单的C/ c++ RubyInline1是目前最简单的解决方案。
Alternatively if you need to call more substatntial C++ code, you can build a ruby extension. Here is a good tutorial
或者,如果您需要调用更多的子状态c++代码,您可以构建一个ruby扩展。这里有一个很好的教程。
#4
2
You need to wrap your c++ code in a C interface and then bind those C functions to ruby methods using rb_define_method()
您需要将c++代码封装到c接口中,然后使用rb_define_method()将这些c函数绑定到ruby方法中
alternatively you can use SWIG, as Aurelien said.
或者你也可以用SWIG,正如Aurelien所说。