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
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
I am not sure if the title of this question gets to the point. I have written a large software system in C C++ for Windows, and want to give the users of this system the option to add compiled code to it. The user should be able to do basic stuff, and exchange data with my program.
Currently the implemented way is via DLLs. But for this, a grown up compiler is needed, and it is not as easy as I wished. Is there a tiny C compiler that can create Windows DLLs?
Another idea is the Java native interface. But this requires a complete Java system to run in the background, and it is not easy to run code in it.
Do you have any other ideas?
Any interpreted language? (TCL and Lua were designed as extension languages, but you can nearly as easily interface with any other).
How about python integration?
You could create an python interface that interfaces with your application. Python is rather easy to learn and should integrate easily with c/c++. The python documentation has an own chapter on that.
You could also use a tool like swig to generate the interface.
The advantage of this is that they wouldn't have to compile anything. They could just supply python files that could be loaded into your application and run within the software. This is a well known use for python, and something its simple syntax promotes.
As the other says you will be best of by providing an embedded language.
I would chip in for javascript and use the google v8 engine
By using javascript you get a language nearly everbody can use and program in.
There is other javascript engines you can embed like SpiderMonkey.
See this answer for what to choose.
An interpreted language is not good enough. I need speed. The software itself is an interpreted language. So I added support for the tiny C compiler. It is only C, and I do check mingw, which probably would not be as tiny as this. Thanks for all your hints.
Added after several months:
I have now two tools, actually: TinyC and Python. The speed difference between those is noticable (factor 5-10), but that usually does not matter too much. Python is much easier for the user, though I managed to integrate both into the Euler GUI quite nicely.
One of the ways is to add scripting. You application can host scripting environment and expose its services there. Users would be able to execute JScript/VBScript scripts and interact with your application. The advantage is that with reasonable effort you can get the power of well known and well documented scripting languages into your application (I suppose there is even debugger for scripting there). You will be required to shape your app services as COM interfaces and scripts will be able to access them automatically using method names you assigned on C++ side.
C++, Win32 and Scripting: Quick way to add Scripting support to your applications
MSDN Entry Point - IActiveScript interface
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 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.
To call C++ code from Ruby, you will likely want to build an extension.
If you are an experienced C++ developer, you may feel comfortable with Rice:
https://github.com/jasonroelofs/rice
It uses C++ metaprogramming techniques to simplify writing extensions.
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.
I believe the questioner is asking how to call C++ from with in Ruby, if so then the for simple C/C++ RubyInline1 is by the far the simplest solution.
Alternatively if you need to call more substatntial C++ code, you can build a ruby extension. Here is a good tutorial
You need to wrap your c++ code in a C interface and then bind those C functions to ruby methods using rb_define_method()
alternatively you can use SWIG, as Aurelien said.
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 :) !