如何使用 Boost.Python
How to use Boost.Python
我最近才发现了Boost.Python,我试图弄清楚它是如何工作的。我尝试在官方网站上浏览教程。但是,我得到了
link.jam: No such file or directory
当像示例中一样运行 bjam 时(这似乎只是一个警告),和
Traceback (most recent call last):
File "hello.py", line 7, in <module>
import hello_ext
ImportError: libboost_python.so.1.55.0: cannot open shared object file: No such file or directory
运行 Python hello.py 时。
我还尝试编译一个模块,如另一个教程中所述,结果类似。我正在运行 Ubuntu14.04,自己编译了 boost1.55。
我尝试编译以下内容:
#include <boost/python.hpp>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
从命令行使用以下命令:
g++ -o hello_ext.so hello.cpp -I /usr/include/python2.7/ -I /home/berardo/boost_1_55_0/ -L /usr/lib/python2.7/ -L /home/berardo/boost/lib/ -lboost_python -lpython2.7 -Wl, -fPIC -expose-dynamic
这仍然给了我一个:
/usr/bin/ld: impossibile trovare : File o directory non esistente
collect2: error: ld returned 1 exit status.
最后,我能够让它工作。首先,我按照Dan的建议修复了链接器问题。它最终编译了,但我仍然得到:
ImportError: libboost_python.so.1.55.0: cannot open shared object file: No such file or directory
问题是 python 模块无法正确加载,所以我需要添加另一个链接器选项。在这里,我报告最终的制作文件:
# location of the Python header file
PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
# location of the Boost Python include files and library
BOOST_INC = ${HOME}/boost/include
BOOST_LIB = ${HOME}/boost/lib
# compile mesh classes
TARGET = hello_ext
$(TARGET).so: $(TARGET).o
g++ -shared -Wl,-rpath,$(BOOST_LIB) -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so
$(TARGET).o: $(TARGET).C
g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).C
请注意 -wl,-rpath 选项,它显然使新创建的共享库可用于 python 脚本。
@Dan:感谢您的宝贵提示。
相关文章:
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 我应该包含什么来制作 boost.python 扩展?
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- C++ 和 Boost.Python - 如何将变量公开给 python 并在循环中更新它?
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- Boost Python Numpy - 要初始化的未定义引用
- Boost.Python :C++模板类型匹配的嵌套命名空间
- Boost.Python 列出了所有公开的类和属性
- Boost.Python 在静态库方面失败
- python boost scope 会导致 NoneType 错误
- TypeError:__init__() 应该返回 None,而不是 Python Boost 的 'NoneType'
- 从C++缓冲区到python::boost::list
- 使用C++缓冲区创建PhotoImage:Python 3.3,Python.Boost和Tkinter
- Boost.Python boost::shared_ptr no to python by value convert
- Boost::Python Boost:的正向声明:Python::对象抛出Python TypeError
- python boost::::列表的长度
- c++ Python Boost中的友元函数
- 用Homebrew在Mac OS X上构建python boost hello world-Makefile vs.Re
- 通过Python BOOST使用派生类(c++)