Pybind11 来自 ostream(Jupyter 笔记本)的 C++ python 标准输出

问题描述 投票:0回答:0

我在显示 show() 函数和 Jupyter notebook 的输出时遇到问题。 这是工作代码的小例子:

#include <pybind11/stl.h>
#include <pybind11/pybind11.h> 
#include <pybind11/iostream.h> 
#include <iostream>       
#include <stdio.h>   
#include <stdlib.h>       
#include <algorithm>
#include <vector>
#include <limits>
#include <math.h>

using namespace std;
namespace py = pybind11;

void show(std::string prompt, double x){
  std::cout << prompt << " " << x << '\n';
}


PYBIND11_MODULE(appname, handle) {

    handle.def("show", (void (*)(std::string, double)) &show);
}

当您从终端在 python 中运行以下命令时,一切正常。 但是,在使用 Jupyter notebook 时,它没有任何显示。

import appname    
appname.show("number:", 5.0)

这里描述了问题(https://pybind11.readthedocs.io/en/stable/advanced/pycpp/utilities.html)但我无法正确实现它。

感谢您的任何建议。

python c++ jupyter-notebook pybind11 ostream
© www.soinside.com 2019 - 2024. All rights reserved.