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

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

问题描述

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

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

如何从Ruby调用C ++函数?

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

推荐答案

您有3种可能:

加载库。即使它有点棘手,你可以决定编写自己的加载器和绑定你的C ++库在Ruby。
这是通过使用扩展模块来完成的。您可以在这里找到一个全面的教程: http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

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)您可以使用工具来生成C ++库周围的Ruby包装器。以SWIG为例( http://www.swig.org/ )。
您只需要创建一个swig特定语法的文件并提供给SWIG。

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)您可以选择使用中间件,例如CORBA / ICE /任何。如果你只想调用一些C ++函数,它可能是有点overkill,但它将允许你远程调用函数,或隐藏网格后面的中间件。

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天全站免登陆