从python运行c ++代码 [英] running c++ code from python
问题描述
我想执行一个代码helloword.cpp,它从控制台接收一些参数来解析这些参数,然后在控制台中输出hello world。
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.
我想从一个python脚本中解析这些参数parsearguments.py
Now, I want to parse these arguments from a python scripts parsearguments.py
例如:
def parse_arguments:
...# some code
return arguments
$ b b
现在,我如何在python和c ++之间进行通信。
我一直在看,看到cython,boost python是选项,但我很难找到正确的简单hello世界的例子。
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
推荐答案
如何将使用Python生成的文本传递到C ++程序的标准输入中?基本上,你必须使用Python的 subprocess
模块启动C ++程序并将文本转储到其标准输出中。
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.
如果您的C ++程序需要在后台单独运行,您可以尝试其他形式的进程间通信,如unix域套接字。
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.
使用boost: :python也是一个选项,但它可能有点更难处理。
Using boost::python is also an option, but it might be a little more difficult to deal with.
这篇关于从python运行c ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!