开发者

C, C++ Interface with Python

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 comm开发者_如何学编程and 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


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+' '+...)


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.


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()


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


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

  • SWIG
  • Boost.Python
  • Cython


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜