python - 在 C++ 中运行 python 对象

标签 python c++

我只是有一个关于在 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/

相关文章:

c++ - 如何使用Opencv计算强度均方差

c++ - 模板精神 X3 解析器

c++ - 关闭数字最左边的非零位

c++ - 如何防止我的程序打印额外的斜杠?

python - 如何使用远程 API 重置数据存储模型的属性类型

python - Bert 模型按自己的方式分割单词

python - 如何使用 Python 将 CSV 字符串加载到 MySQL 中

c++ - 尝试使用 boost::serialization 和 arcive 反序列化对象时得到 "input stream error"

python - 正确使用 `isinstance(obj, class)`

python - 如何将 Python + Electron JS 编译成桌面应用程序(exe)