What's the best way to embed Ruby as a scripting language in C++? Using ruby.h? SWIG? Something else? What I need is to expose some C++ objects to Ruby and have the Ruby interpreter evaluate scripts that access these objects. I don't care about extending Ruby or accessing it in C++.
I've found this article on embedding Ruby in C++, and while it's very useful, it's kinda old and I was wondering if there are any other resources on the subject (or open source implementations of similar engines).
Rice is looking very promising.
Ruby provides a very helpful README.EXT file. It has lots of information about how to extend Ruby, and convert between C & Ruby types.
There is also this excerpt from the pick axe book which pretty much covers the same thing.
In my case, when I added Ruby scripting to my application I decided against using swig, because my needs were very simple, and I didn't want to add yet another build dependency.
swig is probablly the way to go..... but ruby doesnt embed too well......
if you want a language that embeds nicely into C++, try lua
You might wish to check out tinyrb.
I've been working on Rarity (https://github.com/Plaristote/Rarity), which does two things:
Generates Ruby bindings from a YML description of your C++ API
Allows interaction with Ruby script in the most C++ fashion there is
I've solved a good deal of question (exception handling, garbage collection)...
I haven't seen anywhere else the code generation that Rarity uses to make your bindings come to life. I also haven't seen any other lib that allows such an easy conversion between C++ and Ruby types.
I think Rarity's worth the shot ! And I'd be glad to have some feedback as well :) !
Related
I've just started to read about Ruby, and I was wondering if it can be embedded in a C++ application like Lua which provides a library to parse a given script file: https://stackoverflow.com/a/5127294/399107
Yes, you can. You just need to embed a Ruby engine in your application.
Note that, unlike the main Lua engine, some Ruby engines aren't really that well suited to being embedded into other programs. But, for example, Rubinius, IronRuby and JRuby have been specifically designed with embedding in mind, and even though it isn't pretty, you can embed YARV or MRI as well, even though they are not designed for it.
There's also MRuby, but unlike the others, it doesn't implement the full Ruby Language Specification, it only implements a subset of the ISO Ruby Specification which itself is only a small subset of the intersection of Ruby 1.8 and Ruby 1.9. Plus, it hasn't been released yet, as is evidenced by the fact that not even its homepage exists yet. It is, however, specifically designed for embedding, in both senses of the word: being embedded into other programs, and being useful on an embedded device with very little RAM.
As you may have noticed, it is much easier to embed Ruby into your app if the app is running on the Java platform or the CLI. There are C++ compilers for both the Java platform and the CLI, so that option is not entirely out of the question. Otherwise, I'd say that Rubinius is easier to embed, but more people have tried embedding YARV, so there are more blog posts about how to do that. (Or maybe, embedding Rubinius is so trivial nobody needs to write blog posts about it.)
A long time ago, someone was working on an implementation of Ruby for the Lua VM, but that implementation never went anywhere. Would solve all your problems, though :-)
Sure you can. It's possible with with SWIG, or you can make your own bindings for it (or google to see if someone has already done the work). The big question is do you really want to? The ruby interpreter is pretty heavy, and the interface isn't very nice.
Matz is working on an embeddable version of Ruby called mruby, which strives to be as easy to embed and as light as Lua. But its still alpha quality.
Yes, it's possible. Most of the standard libraries types are written in C. And when you can use C, you can use C++ too. Use extern "C" declared functions to get the right binding. I had a lot of trouble, when using a C++ compiler that was different (different version) from the compiler that was used to compile the ruby interpreter.
Here is the part of the pick axt book, that covers the ruby extension library: http://media.pragprog.com/titles/ruby3/ext_ruby.pdf
In an open source C++ web server project, I wrote a ruby / rack adapter, to use the server with rails: https://github.com/TorstenRobitzki/Sioux/tree/master/source/rack
is there anything similar to unity but with the difference that instead of javascript I can use C++?
It is funny, I was looking to learn a little bit of unity as I seen it as requirement for some postings... and I found this question.
I think Ogre3D is great... it may be what you are looking for.
here is the link ogre3d.org
Ogre3D is what I used. http://www.ogre3d.org/
It is somewhat of a misconception that Unity uses Javascript. The so-called Javascript used in Unity is actually a dialect of Javascript that is CLI-compatible and is not exactly the same as the official Javascript. Also, you are not limited to using Javascript in Unity. You can also use C#, which is similar to C++ and Java in some ways or the less commonly used Boo script.
Personally, I would recommend using C# in Unity because it is very powerful and allows calling of native C/C++ functions via P/Invoke. Here is a link to the use of plugins:
http://unity3d.com/support/documentation/Manual/Plugins.html
Link to list of Game Engines. Please take your time to read and consider them:
http://en.wikipedia.org/wiki/List_of_game_engines
If you want accessibility offered by Unity, C++ is certainly wrong choice. Much better would be interpreted/script language, such as Python. They are more flexible in terms of working in web browser/many os-es.
If you're interested in targetting mobile platforms then Marmalade might be a good fit.
Panda3D It's not a think compared to Unity, but it can work as a web plugin, cross-platform. It has a lack of IDE, but it is opensource.
As far as I know, there is no need to use JavaScript or Boo with Unity, and there is nothing wrong with C# acting like script language ( which is really hard with C++ ).
as a unity developer, I liked to add c++ to by knowledge base as all AAA games are made by C++ and I was looking for something like unity but for CPP. the only good cpp engine is unreal that doesn't really support c++. the language is a dialect of c++ and there are big differences. like very
rare usages of pointer and references
more syntaxes like c# attributes(cpp has not attribute)
...
so you don't learn memory management. the nearest thing that I could find was COCOS2dx as the whole framework is written in pure standard c++ and many libraries like UI and Physics are already added. you can write your own engine and editor with it.
Unreal Engine uses C++
default standard is C++14
https://www.unrealengine.com
I am developing a C++ application that needs to be multiple platform compatible (Windows/Linux) and want to grant users the ability to extend the software to fit their needs exactly without allowing them to change critical parts of the application (so I do not want them in the C++ code).
What I am looking for is to embed a scripting language (I would prefer Python because I am familiar with it already, but it's not mandatory), so scripts put in some plugin folder can manipulate objects of the application if I want these objects to be modifyable.
The most simple example: if someone wants to build their own UI for my application, they should be able to do so with such a script.
The problem however is, that I've never put C++ and any kind of external scripts together, so I really do not know how to start. After looking for material to get started, I found that Lua claims to be a good language to do that, but I could not find good beginner tutorials.
I would really appreciate if someone knew a good place to start, be it online resources, or a good book. I wouldn't mind spending a few bucks on a good book.
As a learner, I tend to learn best from a mix of example code and a few lines explaining those.
I would suggest you to read Programming in Lua, this book has an entire section on how to embed Lua in C (and C++).
It is very highly rated by Amazon users.
The language has also pretty good online documentation and a active mailing list.
If you want to use Python I would definitely suggest using Boost.Python. It is an incredibly well designed library. Just an example: all you have to do to expose a C++ class to Python is this:
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
It handles almost everything automatically: conversions between types, exceptions, it even allows you to use reference counted objects between the two languages with boost::shared_ptr.
The article here at linux journal is a good place to start on how to embed a python interpreter in your c/c++ code. This is only half the battle however because when the interpreter is embedded, you then need to publish some part of your software to the scripting environment. The basic API to do so is in C and if most of your code is C++, it might be best to use boost::python since writing C wrappers around your C++ classes might be cumbersome. You can also use Py++ to generate the boost::python binding.
If you only want to use scripting as a door to customization and you can live with the memory footprint of python, it might be a better choice than Lua. Lua is usually good for small environment like game development. There is also a lot more python developers than lua developers as well as more built-ins and third party libraries available.
for Python, I guess the boost library is meant to do it. As for Lua, I haven't used it myself, but a quick Google search first led me to debian admin and then to Lua's C interface. Have you looked into those?
C/C++ would be good option to write some of the performance critical aspects of a Ruby Application. I know this is possible.
I would like to know how to add C/C++ code into Ruby code; any other language for that matter.
Are there any practical applications of this which you noticed in open source projects or else?
Besides "Extending Ruby", here are two other resources:
README.EXT (extension.rdoc) - shows you more about how to build C extensions. A good compliment to "Extending Ruby"
Ruby Inline - This is a library that tries to make it easier to build C extensions by having you call methods in ruby to compile C code.
Look in the "Extending Ruby" section of the Pickaxe book:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html
you should have a look at SWIG - it allows you to create ruby extensions in C/C++.
We use ffi in one of our projects.
Ruby-FFI is a ruby extension for
programmatically loading dynamic
libraries, binding functions within
them, and calling those functions from
Ruby code. Moreover, a Ruby-FFI
extension works without changes on
Ruby and JRuby.
It works quite well.
Compile your high performance code into a system service/executable, and call it from inside Ruby...?
Seems like you need to read up on Extending Ruby
I'm learning Ruby, and I'm starting to play with building extensions in C. I have Programming Ruby The Pragmatic Programmers' Guide and so I can follow that for the basic nuts and bolts. What I was wondering is if there already existed some nifty frameworks/whatever to help interoperability between Ruby and other languages, with C++ being the most important for me. I've tried googling, but the results focus on language comparisons, rather than language interoperability.
TIA,
Andy
Take a look at SWIG. It's a nice framework for integrating C and C++ programs with other programs written in higher level languages. It was originally written to support Python, TCL, and Perl, but has been expanded to support Ruby as well.
If you want to use Ruby with Java, then look into JRuby. It is a Java implementation of Ruby and allows you to call Java libraries/code through Ruby.
http://jruby.org/
FFI is the recommended way to hook Ruby implementations up to C libraries, but a bit of Googling suggests that this probably won't work as-is with C++, so try SWIG. There's an FFI-SWIG thing here.