12

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

frazman
  • 32,081
  • 75
  • 184
  • 269
  • 5
    One doesn't execute C++ source code. – Ignacio Vazquez-Abrams Jun 20 '12 at 06:11
  • Your solution doesn't sound good to me, but maybe I'm missing some context. Why did you decide to parse arguments with Python in the first place ? –  Jun 20 '12 at 06:13
  • @Grigory: I am using one of the frameworks written in c++ while I mostly use python... As of now, I first gather the data and infer something from python.. and then that inference is needed to be passed to that C++ framework. As of now, I am saving everything in a text file in the intermediate step and then read that file to C++ but i want to integrate them together.. what is the best way to do this. Thanks – frazman Jun 20 '12 at 06:15
  • @IgnacioVazquez-Abrams: Hi. Thanks for the comment. Then what is a good way to go around this. Thanks. – frazman Jun 20 '12 at 06:16
  • First, tell us what you *really* have. – Ignacio Vazquez-Abrams Jun 20 '12 at 06:18
  • @IgnacioVazquez-Abrams: copying the comment i just wrote "I am using one of the frameworks written in c++ while I mostly use python... As of now, I first gather the data and infer something from python.. and then that inference is needed to be passed to that C++ framework. As of now, I am saving everything in a text file in the intermediate step and then read that file to C++ but i want to integrate them together.. what is the best way to do this. Thanks" Thanks. – frazman Jun 20 '12 at 06:18

4 Answers4

11

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.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
jmf_zaiecp
  • 321
  • 2
  • 8
8

How about passing whatever text you generate with Python into the standard input of your C++ program? Basically, you have to use Python's subprocess module to fire up the C++ program and dump the text into its standard output.

In case that your C++ program is required to be running separately in the background, you could try another form of interprocess communication, like unix domain sockets.

Using boost::python is also an option, but it might be a little more difficult to deal with.

4

A couple of other options besides Boost.python are SIP and SWIG (Simplified Wrapper and Interface Generator). Like Boost, SIP and SWIG are open source.

SWIG is particularly powerful, but also a bit hairy. It provides support for interfacing C and C++ with a boatload of other languages including (not a complete list) Python, Perl, Lua, Tcl/Tk, Ocaml, Ruby, Java. One aspect of SWIG is that it parses your C++ headers. This has benefits and pitfalls. A benefit is that it does most of the work of generating the interfaces. A downside is that it doesn't handle some of the dark corners of C++ 2003, and it hasn't stepped up to C++11 at all. Another downside is that compilation of a large project becomes slow. Very, very slow.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
2

Using boost.python sounds like a good solution for me. But depending on your C++ experience this can be quite tricky. A good point to start is here:

http://wiki.python.org/moin/boost.python

Boost.Python enables you to export C++ classes and member functions to Python to be able to use them from there.

duselbaer
  • 935
  • 2
  • 6
  • 10