如何从 ruby​​ 中调用 C++ 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1542520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:21:10  来源:igfitidea点击:

How can I call C++ functions from within ruby

c++ruby

提问by

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++ 函数?

回答by NewbiZ

You have 3 possibilities :

你有 3 种可能性:

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-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

2) You can use a toolthat 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/whatever。如果您只想调用一些 C++ 函数,这可能有点矫枉过正,但它允许您远程调用这些函数,或者在中间件后面“隐藏”一个网格。

回答by Paul Brannan

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 代码稍微复杂一些。

回答by hhafez

I believe the questioner is asking how to call C++ from with in Ruby, if so then the for simple C/C++ RubyInline1is by the far the simplest solution.

我相信提问者是在问如何在 Ruby 中使用 with 调用 C++,如果是这样,那么 for simple C/C++ RubyInline 1是迄今为止最简单的解决方案。

Alternatively if you need to call more substatntial C++ code, you can build a ruby extension. Here is a good tutorial

或者,如果您需要调用更多子统计 C++ 代码,您可以构建一个 ruby​​ 扩展。这是一个很好的教程

回答by horseyguy

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 所说。