Ben Ben - 2 years ago 90
Python Question

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!

Answer Source

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>

  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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download