从c ++程序调用python进行分发

Bri*_*ndy 48 c++ python embedded-language

我想从我的c ++程序中调用python脚本文件.

我不确定我将分发给谁的人将安装python.

基本上我正在寻找一个我可以使用的.lib文件,它具有类似Apache的分发许可证.

bha*_*dra 67

我想从我的c ++程序中调用python脚本文件.

这意味着您希望在您的C++应用程序中嵌入Python.正如在另一个应用程序嵌入Python中所提到的:

嵌入Python类似于扩展它,但并不完全.不同之处在于,当你扩展Python时,应用程序的主程序仍然是Python解释器,而如果嵌入Python,主程序可能与Python无关 - 相反,应用程序的某些部分偶尔会调用Python解释器运行一些Python代码.

我建议您首先在另一个应用程序中嵌入Python.然后参考以下示例

  1. 在C/C++中嵌入Python:第一部分

  2. 在C/C++中嵌入Python:第二部分

  3. 在多线程C/C++应用程序中嵌入Python

如果你喜欢Boost.Python,你可以访问以下链接:

  1. 使用Boost.Python嵌入Python第1部分

  • 最终链接已损坏 (2认同)

roo*_*roo 32

Boost有一个python接口库,可以帮助你.

Boost.Python的


ing*_*net 8

有趣的是,还没有人提到pybind11。从他们的文档中:

pybind11是一个轻量级的仅标头的库,它公开了Python中的C ++类型,反之亦然,主要是创建现有C ++代码的Python绑定。它的目标和语法与David Abrahams出色的Boost.Python库相似:通过使用编译时自省来推断类型信息,以最大程度地减少传统扩展模块中的样板代码。自创建以来,该库在很多方面都超越了Boost.Python,在许多常见情况下导致绑定代码大大简化。

具体来说,调用Python函数(称为嵌入)就这么简单(从文档中获取):

#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;

int main() {
    py::scoped_interpreter guard{}; // start the interpreter and keep it alive
    py::print("Hello, World!"); // use the Python API
}
Run Code Online (Sandbox Code Playgroud)

  • 哇,我很幸运一直向下滚动直到这个答案! (4认同)

小智 7

使用系统调用从 C++ 运行 python 脚本

#include<iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int result = system("/usr/bin/python3 testGen1.py 1");
cout << result; 
}
Run Code Online (Sandbox Code Playgroud)