未定义的Boost Python符号:boost::python::detail::init_module
我在尝试导入一个用 Boost Python 编译的扩展时,遇到了一个未定义符号的错误,而这个符号应该是 Boost 库里包含的。
我使用的是 Boost 1.46.1、Python 3.1.2 和 GCC 4.4.5。
我用以下命令构建了 Boost:
$ ./bootstrap.sh --with-python-version=3.1
$ sudo ./bjam -j4 install
然后我编译了一个简单的 Boost Python 库:
#include <boost/python.hpp>
struct mystruct {
int i;
};
BOOST_PYTHON_MODULE(test) {
using namespace boost::python;
class_<mystruct>("Mystruct")
.def_readwrite("i", &mystruct::i)
;
}
使用的命令是:
$ g++ -shared question.cpp -I/usr/include/python3.1 -lboost_python3 -lpython3.1 -otest.so
这个过程没有出错。
接着我尝试在 Python 中运行它,但似乎找不到 Boost Python 应该提供的 init_module 函数:
$ python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./test.so: undefined symbol: _ZN5boost6python6detail11init_moduleEPKcPFvvE
ldd 报告了以下内容:
$ ldd -r test.so
linux-gate.so.1 => (0x00ab3000)
libboost_python3.so.1.46.1 => /usr/local/lib/libboost_python3.so.1.46.1 (0x002fe000)
libpython3.1.so.1.0 => /usr/lib/libpython3.1.so.1.0 (0x005dc000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x001f8000)
libm.so.6 => /lib/libm.so.6 (0x00110000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00424000)
libc.so.6 => /lib/libc.so.6 (0x00886000)
libutil.so.1 => /lib/libutil.so.1 (0x00e13000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00136000)
libdl.so.2 => /lib/libdl.so.2 (0x00349000)
librt.so.1 => /lib/librt.so.1 (0x00150000)
libssl.so.0.9.8 => /lib/libssl.so.0.9.8 (0x00553000)
libcrypto.so.0.9.8 => /lib/libcrypto.so.0.9.8 (0x00ab4000)
libffi.so.5 => /usr/lib/libffi.so.5 (0x00159000)
libz.so.1 => /lib/libz.so.1 (0x00160000)
libexpat.so.1 => /lib/libexpat.so.1 (0x00175000)
/lib/ld-linux.so.2 (0x00495000)
undefined symbol: _ZN5boost6python6detail11init_moduleEPKcPFvvE (./test.so)
编辑:
nm
确认了 /usr/local/lib/libboost_python3.so.1.46.1
确实包含 init_module,但错误依然存在:
$ nm /usr/local/lib/libboost_python3.so.1.46.1 | c++filt | grep init_module
00031a00 T boost::python::detail::init_module(PyModuleDef&, void (*)())
1 个回答
6
未定义的符号是
boost::python::detail::init_module(char const*, void (*)())
不是
boost::python::detail::init_module(PyModuleDef&, void (*)())
在 http://www.boost.org/doc/libs/1_46_1/boost/python/module_init.hpp 上,我发现这个方法的签名在Python 3中已经改变了。
你需要确保在处理boost python头文件时,PY_VERSION_HEX
被正确设置。
在我的系统上,我看到这个变量是在 /usr/include/python3.1/patchlevel.h
中定义的(不过我首先需要安装Python 3.1的开发包)。