Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running c++ code from python

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

like image 681
frazman Avatar asked Jun 20 '12 06:06

frazman


1 Answers

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.

like image 117
jmf_zaiecp Avatar answered Nov 15 '22 16:11

jmf_zaiecp