我只是有一个关于在 C++ 中运行 python 对象的问题。让我在下面的例子中解释我想做什么。
int main () {
// as you know, the python object has its member variables etc, and
// I want it to be initialized only once
py = python_object();
while (1) {
// here, I am hoping to call a member function from the object continously
// and get a return value.
int abc = py.get_num();
if (abc < 0) break;
do_something_in_my_cpp_code();
}
}
我在这里谈论的 python 对象 Capture oOjbect .
我想知道如果你们有任何处理此类事情的经验,这会有多困难。我也对其他可能的方法持开放态度,例如反向(在 python 脚本中运行 C++ 对象)。谢谢!
最佳答案
解决方案 1 创建一个小的 python 脚本来调用您的函数并打印结果。 然后,在c++程序中使用popen调用python脚本,获取显示值。
解决方案 2 在你的 C++ 程序中嵌入 python https://docs.python.org/2/extending/embedding.html
如果您只有很少的 python 调用,我认为解决方案 1 是更快、更安全的方法。
关于python - 在 C++ 中运行 python 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577279/