libboost_python3.so.1.56.0:未定义的符号:PyClass_Type

Iva*_*iuk 6 c++ boost cmake boost-python python-3.x

我正在尝试使用boost :: python库在C++中为Python3创建一个helloWorld模块.

这是一个CmakeList.txt:

set(Python_ADDITIONAL_VERSIONS 3.4)
find_package( PythonLibs 3.4 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )

find_package( Boost 1.56.0 EXACT COMPONENTS python3 REQUIRED )
include_directories( ${Boost_INCLUDE_DIR} )

# Define the wrapper library that wraps our library
add_library( hello SHARED main.cpp )
target_link_libraries( hello ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES} )

# don't prepend wrapper library name with lib
set_target_properties( hello PROPERTIES PREFIX "" OUTPUT_NAME hello)
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <boost/python.hpp>

char const* greet( )
{
    return "Hello world";
}    
BOOST_PYTHON_MODULE(mymodule)
{
    using namespace boost::python;
    def( "greet", greet );
}
Run Code Online (Sandbox Code Playgroud)

我从这里描述的源安装了boost库,但它不允许我使用boost-python3库(在Cmake中有错误).为此我用过

./bootstrap.sh --with-python-version=3.4 --prefix=/usr/local
Run Code Online (Sandbox Code Playgroud)

代替

./bootstrap.sh --prefix=/usr/local
Run Code Online (Sandbox Code Playgroud)

明确指定python的版本;

作为输出,我们得到一个共享库hello.so.一切似乎都没问题.但...

当我尝试将库导入到sript.py包含内容的python脚本时:

import hello
Run Code Online (Sandbox Code Playgroud)

在终端使用命令... $ python3 script.py

我收到一个错误

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    import hello 
ImportError: /usr/local/lib/libboost_python3.so.1.56.0: undefined symbol: PyClass_Type
Run Code Online (Sandbox Code Playgroud)

问题是:如何使boost库与python3兼容?没有问题python2.但我需要python3.当发生同样的错误时我也看到了页面,但它对我没有帮助.

我的软件:

  • 提升版本1.56.0
  • pyhton 3.4
  • cmake版本2.8.12.2
  • gcc 4.8.2
  • 操作系统:Ubuntu 14.04 LTS,64位

Tan*_*ury 21

正如这个答案所述:

PyClass_Type是Python 2 C API的一部分,而不是Python 3 C API的一部分.因此,Boost.Python库可能是针对Python 2构建的.但是,它是由Python 3解释器加载的,其中PyClass_Type不可用.

libboost_python3.so没有提供用于生成的确切过程,因此我只能推测非干净的构建,例如使用Python2构建Boost.Python,然后使用Python3重新配置引导程序,然后使用Python2目标文件构建Boost.Python.无论如何,使用Python3 验证Boost.Python 的干净构建.

$ ./bootstrap.sh --with-python=/usr/bin/python2
...
Detecting Python version... 2.7
$ ./b2 --with-python --buildid=2 # produces libboost_python-2.so
$ ./bootstrap.sh --with-python=/usr/bin/python3 --with-python-root=/usr
...
Detecting Python version... 3.3
$ ./b2 --with-python --buildid=3noclean # produces libboost_python-3noclean.so
$ ./b2 --with-python --clean
$ ./b2 --with-python --buildid=3 # produces libboost_python-3.so

$ nm -D stage/lib/libboost_python-2.so | grep PyClass_Type
                 U PyClass_Type
$ nm -D stage/lib/libboost_python-3noclean.so | grep PyClass_Type
                 U PyClass_Type
$ nm -D stage/lib/libboost_python-3.so | grep PyClass_Type
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,libboost_python-2.so引用PyClass_Type符号.此外,它 libboost_python-3noclean.so包含对PyClass_Type使用libboost_python-2.so目标文件构建的引用.使用干净的构建,libboost_python-3.so不应包含引用PyClass_Type.