Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Friday, August 3, 2012

Invoking native C/C++ libs using ruby

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html...here is what you have to do with ruby

install ffi. linux distros have packages.....so on ubuntu apt-get install ffi...

then here is the module code as well as sample script

module LibMylib
        extend FFI::Library
        ffi_lib "libmylib.so"
        attach_function :MYLIB_helloWorld, [], :void
end
then your script. make sure libmylib is replaced with whatever u named the module file above
require 'ffi'
require 'libmylib'

LibMylib.MYLIB_helloWorld()

Making C++ available through native calls

So if you need some raw of performance in your application what do you do? You drop down into a native development language like C. Well what if you want to code with OOP goodness and use C++. Regular calls from Java, Ruby, etc. will not get to your C++ functions because C++ adds all sorts of template madness when it exports your programs symbols.

So what do you do?


extern "C"

so if your want to wrap your c++ classes in a single method called helloWorld (or MYLIB_helloWorld to not be confused with other helloWorlds!):
extern "C" MYLIB_helloWorld();
in your header file.....then in your cpp file you can just do the normal
MYLIB_helloWorld();
Yay! Now you can make native calls from higher level languages like java or even ruby without them getting pissed off at the c++ template symbols. Will post examples soon.

Monday, July 16, 2012

packit4me.com (2D/3D Bin Packing)

(UPDATE 06/04/2013) This project is now available @ http://www.packit4me.com/
3D/2D bin packing. Given multiple bins and items to be packed into the bins, the algorithm attempts to find a good solutions of how to pack. I got so many requests for this that I made it into a real life project. Check out the site. It has comprehensive documentation on how to call the API with all the popular languages (Java, php, C#, ruby, python). I hope it works for you! If not just email me or make some posts here. I'll try to setup some ticket system soon to take requests.