14

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

6
  • 5
    One doesn't execute C++ source code. Commented Jun 20, 2012 at 6: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 ? Commented Jun 20, 2012 at 6: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 Commented Jun 20, 2012 at 6:15
  • @IgnacioVazquez-Abrams: Hi. Thanks for the comment. Then what is a good way to go around this. Thanks. Commented Jun 20, 2012 at 6:16
  • First, tell us what you really have. Commented Jun 20, 2012 at 6:18

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.