开发者

Embedding Ruby in a C++ application using SWIG?

I've successfully created Ruby-C++ bindings in the past using SWIG where the C++ code was compiled as a dynamic library with the Ruby script connecting to it.

However, I'd like to do it the other way arou开发者_如何学JAVAnd. Create an executable using C++ and enable it to load and execute Ruby code. Ruby should be able to call functions defined on the C++ side as well (naturally, otherwise all I would need is the 'system()' call.)

Does SWIG provide the means to achieve this?


You may be interested in Ruby embedded into c++


SWIG solves part of your problem: it lets you create Ruby bindings to C++ functions that you've written. The other half of your problem, making a C++ program that can evaluate Ruby code, can be as easy or complicated as you wish.

The easy way is to divide your program into two halves: a ruby-accessible part that lives in a dll, and an inaccessible part that links with that dll and invokes Ruby via system().

At a slightly higher level, you can use ruby's C API from C++ to run Ruby code. This gives you better control over how Ruby code is executed, and offers you the opportunity to do things like wrap all executed Ruby in blocks that generate C++ exceptions from Ruby exceptions. The downside here is that you will need to understand Ruby's C API, which is not awful but certainly isn't as friendly as, say, Lua's.

If you want more than that, you're into deep waters. Ruby and C++ are both complicated languages, and getting them to interoperate is a real challenge. Éric Malenfant's link may be of some use to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜