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
.当发生同样的错误时我也看到了页面,但它对我没有帮助.
我的软件:
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
.
归档时间: |
|
查看次数: |
10276 次 |
最近记录: |