如何从 ruby​​ 中调用 C++ 函数 [英] How can I call C++ functions from within ruby

查看:48
本文介绍了如何从 ruby​​ 中调用 C++ 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名经验丰富的 C/C++ 开发人员,但我是 Ruby 的新手.

如何在 Ruby 中调用 C++ 函数?

解决方案

你有 3 种可能性:

1) Ruby 能够加载库.即使有点棘手,您也可以决定编写自己的加载程序并在 Ruby 中绑定您的 C++ 库.这是使用所谓的扩展模块完成的.您可以在此处找到综合教程:http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

2) 您可以使用工具来围绕您的 C++ 库生成 Ruby 包装器.以 SWIG 为例(http://www.swig.org/).您只需要以特定于 swig 的语法创建一个文件并将其提供给 SWIG.然后它将能够为包括 Ruby 在内的多种语言生成包装器.

3) 您可以选择使用中间件,例如 CORBA/ICE/whatever.如果你只想调用一些 C++ 函数可能有点矫枉过正,但它可以让你远程调用这些函数,或者在中间件后面隐藏"一个网格.

I am an experienced C/C++ developer but I am a novice in Ruby.

How can I call a C++ function from with in Ruby?

解决方案

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

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.

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.

这篇关于如何从 ruby​​ 中调用 C++ 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆