Need help getting started with Boost.Python

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:02:13

When this particular linker error occurs, it is often the result of the application building against one version of Python, such as Python 3.x header files, while the boost_python library was built against a difference version, such as 2.x.

In boost/python/module_init.hpp, the init_module function has the following signature when building against Python 3.x:

PyObject* boost::python::detail::init_module(PyModuleDef&, void(*)());

and the following signature when building against Python 2.x:

PyObject* boost::python::detail::init_module(char const* name, void(*)());

As can be seen in the implementation , only one of the functions will be present in the Boost.Python library. Thus, given the Boost.Python library is being linked in, and the linker is only complaining about not being able to resolve the 3.x init_module function, then it is very likely that the Boost.Python library was built against a Python 2.x version, while the application code has been built against Python 3.x header files. You can verify this by dumping the Boost.Python library's symbols and check the init_module signature.

To resolve this, build the application with the same version of Python from which Boost.Python was built. In this case, either:

  • Build the application with Python 2.x header files and link against Python 2.x libraries.
  • Build Boost.Python against Python 3.x. This documentation describes the steps to build Boost, and this documentation goes into detail for Boost.Python. It may be necessary to explicitly provide the Python executable from which Boost.Python will build against during the bootstrap process by using the --with-python argument.

You are missing the Python development headers. Your Linux distribution should have a package for them. (For example, python-dev on Debian or Ubuntu.)

You are building a shared library, because that is what a binary Python module is. For that, you need -shared or -dynamic (check the docs) and you should not have a main() function.

Further, if that doesn't help and you still have the linker errors, use "objdump -T --demangle /path/to/lib" to find out which symbols a library contains and whether it has those that you need. Also check the output of "ldd", which lists dependent shared objects. This should give you a hint which library or libraries to link.

On my system, I also have a program called "python-config" and "python2.7-config". Check if you have something similar, because this script knows which libraries to link at least for python. A similar tool is pkg-config, which is more general and might provide information for boost, too.

Not sure if this is the way to do it, but it seems that the PY_VERSION_HEX is set wrong. What happens if you

#define PY_VERSION_HEX 0x03300000

before including the boost.python headers in your BoostPythonTest.cpp?

kirbyfan64sos

You also have to link with Boost.System, as well as any other libraries mentioned by the linker. Also, try other compilers, such as Clang(https://svn.boost.org/trac/boost/ticket/7536), which comes with XCode(Latest Clang location in XCode 4.5). Also, try posting your makefile. Another attempt would be using bjam/b2 instead of make.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!