如何在Python和C ++之间交换数据

时间:2015-05-25 09:38:12

标签: python c++

我想用Python编写一个程序来读取2个数字(来自文本文件或来自用户输入)调用带有2个参数的C ++函数(2个数字)添加它们并将结果返回给Python程序。我已经设法使用文本文件(从那里保存python中的数字,然后在C ++程序中调用该文件)。但我想直接这样做而不必使用文本文件作为媒介。我希望能够调用该函数并直接发送它的参数,而不是让它从文本文件中读取它们,我希望将输出返回到原始的python代码。

关于如何进行的任何想法,因为我在这个领域相当新鲜?

4 个答案:

答案 0 :(得分:1)

如果你有两个不同的程序,你需要使用inter-process communication的技术(管道,套接字,信号等)。

如果要将C ++代码集成到python程序,可以使用Boost.Pythonthis document

答案 1 :(得分:1)

用于集成Python和C ++的非常强大的工具称为sip,它是Qt绑定PyQt所基于的工具。

您可以直接调用函数甚至将C ++类映射到Python(包括支持在Python中导出C ++类)。

答案 2 :(得分:1)

Cython从版本0.13开始支持C ++。

一些阅读材料:

答案 3 :(得分:-1)

将参数从c ++发送到python

的代码
#include <windows.h>
 {
   system("start abc.py hi"); //here hi is argument sent to abc.py, you can always add more.

 return 0;
 }

Python代码(将其保存为abc.py)

 import sys

 total = len(sys.argv)
 cmdargs = str(sys.argv)

 print ("The total numbers of args passed to the script: %d " % total)
 print ("Args list: %s " % cmdargs)


 # Parsing args one by one 
 print ("Script name: %s" % str(sys.argv[0]))
 print ("First argument: %s" % str(sys.argv[1]))
 print ("Second argument: %s" % str(sys.argv[2]))

阅读更多内容 - (http://www.cyberciti.biz/faq/python-command-line-arguments-argv-example/

相关问题