如何使用Boost::P ython打印到Python终端
How to print to Python terminal with Boost::Python
我想从我用C++编写的库中用python进行相当于打印。我正在使用Boost 1.60.0和Python 2.7。
我找到了以下网站:Mantid和WikiBooks。据我所知,这段代码应该可以工作,但没有打印任何内容。
CPP 文件
void greet()
{
std::cout<<"test_01n";
std::cout<<"test_02"<<std::endl;
printf("test_03");
}
BOOST_PYTHON_MODULE(PythonIntegration)
{
def("greet", greet);
}
py 文件
import PythonIntegration
PythonIntegration.greet()
我检查了该函数是否通过使其返回某些内容来调用并且它可以工作,但仍然没有打印任何内容。
谢谢你的帮助
这个hello world示例似乎完全符合您的要求:https://en.wikibooks.org/wiki/Python_Programming/Extending_with_C%2B%2B
基本上。。。
C++
#include <iostream>
using namespace std;
void say_hello(const char* name) {
cout << "Hello " << name << "!n";
}
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("say_hello", say_hello);
}
现在,在 setup.py
#!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension
setup(name="PackageName",
ext_modules=[
Extension("hello", ["hellomodule.cpp"],
libraries = ["boost_python"])
])
现在你可以这样做:
python setup.py build
然后在python命令提示符下:
>>> import hello
>>> hello.say_hello("World")
Hello World!
相关文章:
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 如何在linux终端中同时编译和运行c++代码
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- IPC使用多个管道和分支进程来运行Python程序
- 从python中调用C++函数并获取返回值
- 终端不会为C++文件创建.exe文件吗
- Python 3.7 和 excess_args 的 SWIG 问题
- Python中的for循环与C++有何不同
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用Pybind11向Python公开Eigen::张量
- Python str to C++ to Python str
- 如何使用Python从C++中读取谷物序列化数据
- 命令在终端或程序中提供不同的行为(Python和C )
- 如何使用python skd与彭博终端连接
- 如何使用Boost::P ython打印到Python终端
- 如何使用c++/python程序在终端中运行程序,然后在该程序中执行语句