running c++ code from python

frazman picture frazman · Jun 20, 2012 · Viewed 28.7k times · Source

I want to execute a code helloword.cpp which takes in some argument from console parses those arguments and then prints "hello world" in the console.

Now, I want to parse these arguments from a python scripts parsearguments.py

So for example:

def parse_arguments:
  ...# some code
  return arguments

Now, how do i communicate between python and c++. I have been reading and see that cython, boost python are the options but I have a hard time finding the right simple hello world example.

Any suggestions will be appreciated. Thanks

Answer

jmf_zaiecp picture jmf_zaiecp · Jun 20, 2012

To execute C++ code in python, you could effectively use boost python, here is a tutorial: http://www.boost.org/doc/libs/1_59_0/libs/python/doc/index.html You write a kind of wrapper outside you C++ code.

If it is C code, python has internal library called ctypes.

In both case, you should compile the C/C++ code into shared library.