Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking languages

I asked a question earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C++, but that Python / Perl / Ruby would be good for the interface bits.

So, this leads me on to another question. How easy is it to link these languages together? And which combination works best? So, if I wanted to have a Ruby CGI-type program calling C++ or Java AI functions, is that easy to do? Any pointers for where I look for information on doing that kind of thing? Or would a different combination be better?

My main experience with writing web applications started with C++ CGI and then moved on to Java servlets (about 10 years ago) and then after a long gap away from programming I did some PHP. But I've not had experience of writing a web application in a scripting language which then calls out to a compiled language for the speed-critical bits. So any advice will be welcome!

like image 877
Ben Avatar asked Nov 07 '08 08:11

Ben


2 Answers

Boost.Python provides an easy way to turn C++ code into Python modules. It's rather mature and works well in my experience.

For example, the inevitable Hello World...

char const* greet()
{
  return "hello, world";
}

can be exposed to Python by writing a Boost.Python wrapper:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)
{
  using namespace boost::python;
  def("greet", greet);
}

That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session:

>>> import hello_ext
>>> print hello.greet()
hello, world

(example taken from boost.org)

like image 160
Pieter Avatar answered Sep 18 '22 13:09

Pieter


First, a meta comment: I would highly recommend coding the entire thing in a high-level language, profiling like mad, and optimizing only where profiling shows it's necessary. First optimize the algorithm, then the code, then think about bringing in the heavy iron. Having an optimum algorithm and clean code will make things much easier when/if you need to reimplement in a lower-level language.

Speaking for Python, IronPython/C# is probably the easiest optimization path.

CPython with C++ is doable, but I find C a lot easier to handle (but not all that easy, being C). Two tools that ease this are cython/pyrex (for C) and shedskin (for C++). These compile Python into C/C++, and from there you can access C/C++ libraries without too much ado.

I've never used jython, but I hear that the jython/Java optimization path isn't all that bad.

like image 42
Ryan Ginstrom Avatar answered Sep 16 '22 13:09

Ryan Ginstrom