小编jai*_*ysc的帖子

pybind11 将 python sys.stdout 从 print() 重定向到 c++

我有一个带有 pybind11 嵌入式 python 解释器的 c++ 程序,执行以下 python 文件,它直接打印到std::cout

# test.py
print("text")
Run Code Online (Sandbox Code Playgroud)

执行该文件的c++程序:

#include <pybind11/embed.h>

namespace py = pybind11;

int main() {
    py::scoped_interpreter guard{};
    py::eval_file("test.py");
}
Run Code Online (Sandbox Code Playgroud)

我发现的其他解决方案需要修改 python 文件 - 如何将 python sys.stdout 作为 std::string 重定向到 c++,不仅使用 print() 语句修改 python 代码?

c++ python pybind11

5
推荐指数
1
解决办法
3794
查看次数

标签 统计

c++ ×1

pybind11 ×1

python ×1