简体   繁体   English

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

[英]libboost_python3.so.1.56.0: undefined symbol: PyClass_Type

I'm trying to create a helloWorld module for Python3 in C++ using boost::python library. 我正在尝试使用boost :: python库在C ++中为Python3创建一个helloWorld模块。

Here is a CmakeList.txt : 这是一个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)

main.cpp main.cpp中

#include <boost/python.hpp>

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

I installed boost libraries from source like described here , but it does not allow me to use boost-python3 library (have an error in Cmake). 我从这里描述的源安装了boost库,但它不允许我使用boost-python3库(在Cmake中有错误)。 For this purpose I used 为此我用过

./bootstrap.sh --with-python-version=3.4 --prefix=/usr/local

instead of 代替

./bootstrap.sh --prefix=/usr/local

to explicitly specify version of python; 明确指定python的版本;

As an output we get a shared library hello.so . 作为输出,我们得到一个共享库hello.so All seems to be ok. 一切似乎都没问题。 But... 但...

When I try to import the library to python script sript.py with content: 当我尝试将库导入到包含内容的python脚本sript.py时:

import hello

in terminal using command ...$ python3 script.py 在终端使用命令... $ python3 script.py

I receive an error 我收到一个错误

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

The question is: How to make boost library compatible with python3? 问题是:如何使boost库与python3兼容? There are no problems with python2 . python2没有问题。 But I need python3 . 但我需要python3 I also saw the page when the same error happens but it didn't help me. 当发生同样的错误时我也看到了页面 ,但它对我没有帮助。

My software: 我的软件:

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

As noted in this answer : 正如这个答案所述

PyClass_Type is is part of the Python 2 C API and not part of the Python 3 C API. PyClass_Type是Python 2 C API的一部分,不是Python 3 C API的一部分。 Hence, the Boost.Python library was likely built against Python 2. However, it is being loaded by a Python 3 interpreter, where the PyClass_Type is not available. 因此,Boost.Python库可能是针对Python 2构建的。但是,它是由Python 3解释器加载的,其中PyClass_Type不可用。

The exact procedure used to produce libboost_python3.so is not presented, so I can only speculate a non clean build, such as building Boost.Python with Python2, then reconfiguring bootstrap with Python3, and then building Boost.Python with the Python2 object files. 没有提供用于生成libboost_python3.so的确切过程,因此我只能推测非干净的构建,例如使用Python2构建Boost.Python,然后使用Python3重新配置引导程序,然后使用Python2目标文件构建Boost.Python。 Regardless, verify a clean build of Boost.Python with Python3. 无论如何,使用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

As expected, libboost_python-2.so references the PyClass_Type symbol. 正如所料, libboost_python-2.so引用了PyClass_Type符号。 Additionally, the libboost_python-3noclean.so contains a reference to PyClass_Type as it was built with libboost_python-2.so 's object files. 此外, libboost_python-3noclean.so包含一个参考PyClass_Type ,因为它是内置libboost_python-2.so的目标文件。 With a clean build, libboost_python-3.so should not contain a reference to PyClass_Type . 使用干净的构建, libboost_python-3.so不应包含对PyClass_Type的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM