如何将提升模块从C++导入到python
How to import boost module from C++ to python?
以下是我要导入的c ++代码。
#include <boost/python.hpp>
#include <string>
/*
* This is the C++ function we write and want to expose to Python.
*/
const std::string hello() {
return std::string("hello, zoo");
}
/*
* This is a macro Boost.Python provides to signify a Python extension module.
*/
BOOST_PYTHON_MODULE(zoo) {
// An established convention for using boost.python.
using namespace boost::python;
// Expose the function hello().
def("hello", hello);
}
以下代码是 python 脚本。
import zoo # In zoo.cpp we expose hello() function, and it now exists
in the zoo module.
assert 'hello' in dir(zoo) # zoo.hello is a callable.
assert callable(zoo.hello) # Call the C++ hello() function from Python.
print zoo.hello()
当我尝试运行脚本时,我在终端上没有收到"你好,动物园"作为输出。我哪里犯了错误?
以下是我收到的错误消息:
导入:未授权zoo' @ error/constitute.c/WriteImage/1028.
./visit_zoo.py: line 3: syntax error near unexpected token
('./visit_zoo.py:第 3 行:"在 dir(zoo)中断言'hello'"
你不是忘记指出脚本应该由 Python 运行吗,就像我一样?
您可以在脚本文件的标头中包含 python 可执行文件:
#!/usr/bin/env python2
并使文件可执行或使用 Python 调用脚本:
$ python <filename>
相关文章:
- 尝试导入pybind-opencv模块时出现libgtk错误
- 导入库可以跨dll版本工作吗
- 建议在运行时将带有类实例的列表从c++导入qml
- 导入/导出变量时出错
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 将QT项目导入Visual Studio 2019会给我带来很多LNK2001错误
- 是否可以将函数导入命名空间,但不能导出它?
- 如何将 CLion C++ 项目导入 Visual Studio?
- Python 3.8 不能与 Pybind11 一起导入 [Windows]
- 无法将标准库导入本机 android 项目中的头文件
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 在 python 模块中导入子模块时PyImport_Import失败
- 将 C# DLL 导入 C++ 以用于 JNI
- 使用 QSslCertificate 在 Qt 中正确导入 pkcs12
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 编译boost_python扩展无法在 python 2.7 中导入
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- Dll在同一解决方案中从 c# 项目导入到 c++ 项目
- C++20 使用 Visual Studio 编译模块:不编译或导入 ixx 文件
- 我可以将cocos项目导入虚幻4吗?