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.然后参考以下示例
如果你喜欢Boost.Python,你可以访问以下链接:
有趣的是,还没有人提到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)
小智 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)