5

I have c++ code that has grown exponential. I have a number of variables (mostly Boolean) that need to be changed for each time I run my code (different running conditions). I have done this using the argument command line inputs for the main( int argc, char* argv[]) function in the past.

Since this method has become cumbersome (I have 18 different running conditions, hence 18 different argument :-( ), I would like to move to interfacing with python (if need be Bash ). Ideally I would like to code a python script, where I set the values of data members and then run the code.

Does anyone have a any pointer/information that could help me out? Better still a simple coded example or URL I could look up.

Edit From Original Question:

Sorry I don't think I was clear with my question. I don't want to use the main( int argc, char* argv[]) feature in c++. Instead of setting the variables on the command line. Can I use python to declare and initialize the data members in my c++ code?

Thanks again mike

MWright
  • 1,681
  • 3
  • 19
  • 31
  • Do you want your script just to read input from console or GUI and then launch your program with gathered parameters? – Andrey Sboev May 11 '11 at 07:49

6 Answers6

8

Use subprocess to execute your program from python.

import subprocess as sp
import shlex

def run(cmdline):
  process = sp.Popen(shlex.split(cmdline), stdout=sp.PIPE, stderr=sp.PIPE)
  output, err = process.communicate()
  retcode = process.poll()
  return retcode, output, err

run('./a.out '+arg1+' '+arg2+' '+...)
log0
  • 10,489
  • 4
  • 28
  • 62
6

Interfacing between C/C++ and Python is heavily documented and there are several different approaches. However, if you're just setting values then it may be overkill to use Python, which is more geared toward customising large operations within your process by farming it off to the interpreter.

I would personally recommend researching an "ini" file method, either traditionally or by using XML, or even a lighter scripting language like Lua.

Antony Woods
  • 4,415
  • 3
  • 26
  • 47
  • Hi acron, It looks like you are right. ini file method does suit the scale of my project. Thanks for your help. I have to say, all the other answers and advice are superb :-D . Thanks a lot guys!!!!!! – MWright May 12 '11 at 14:14
3

You can use subprocess module to launch an executable with defined command-line arguments:

import subprocess

option1 = True
option2 = Frue
# ...
optionN = True

lstopt = ['path_to_cpp_executable', 
           option1,
           option2,
           ...
           optionN 
         ]
lstopt = [str(item) for item in lstopt] # because we need to pass strings

proc = subprocess.Popen(lstrun, close_fds = True)
stdoutdata, stderrdata = proc.communicate()

If you're using Python 2.7 or Python 3.2, then OrderedDict will make the code more readable:

from collections import OrderedDict
opts = OrderedDict([('option1', True),
                    ('option2', False), 
                   ]

lstopt = (['path_to_cpp_executable'] + 
          list(str(item) for item in opts.values())
         )

proc = subprocess.Popen(lstrun, close_fds = True)
stdoutdata, stderrdata = proc.communicate()
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
2

With the ctypes module, you can call arbitrary C libraries.

phihag
  • 278,196
  • 72
  • 453
  • 469
2

There are several ways for interfacing C and C++ code with Python:

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
2

I can only advise to have a look at swig : using director feature, it allows to fully integrate C++ and python, including cross derivation from onle language to the other

Bruce
  • 7,094
  • 1
  • 25
  • 42