简体   繁体   English

使用多个Python版本构建Boost

[英]Build Boost with multiple Python versions

I use several versions of Python on my computer : 2.6.6 , 2.7.6 and 2.7.9 . 我使用Python的几个版本我的电脑上: 2.6.62.7.62.7.9 When I compile Boost with boost-python, I have to give the Python to use in argument. 当我用boost-python编译Boost时,我必须让Python在参数中使用。 If I want compatibility, have I to compile Boost for each Python version ? 如果我想要兼容性, 我是否要为每个Python版本编译Boost? (it's quite huge !) Or is there a way to use only one build of Boost with several python versions ? (这是非常巨大的!)或者有没有办法只使用一个Boost版本和几个python版本?

The official Python development cycle does not describe the stability of the application binary interface (ABI) between releases. 官方Python 开发周期没有描述版本之间应用程序二进制接口(ABI)的稳定性。 For releases before Python 3.2, there is no guarantee for ABI compatibility. 对于Python 3.2之前的版本,无法保证ABI兼容性。 For 3.2 and beyond, PEP 384 defines the stable ABI where a subset of the Python/C API is guaranteed to maintain ABI compatibility. 对于3.2及更高版本, PEP 384定义了稳定的ABI ,其中保证Python / C API的子集保持ABI兼容性。 To use this subset, Py_LIMITED_API needs to be defined when building Boost.Python and extension modules. 要使用此子集,需要在构建Boost.Python和扩展模块时定义Py_LIMITED_API

While it primarily depends on the Python/C API types and functionality being used directly in user code or through Boost.Python, in general: 虽然它主要依赖于直接在用户代码中使用的Python / C API类型和功能,或者通过Boost.Python,但一般来说:

  • for major-level releases, such as Python 2 and Python 3, Boost.Python and user code will need to be recompiled 对于主要版本,例如Python 2和Python 3,Boost.Python和用户代码将需要重新编译
  • for minor-level releases, such as Python 2.6 and Python 2.7, Boost.Python and user code may need to be recompiled 对于次级版本,例如Python 2.6和Python 2.7,Boost.Python和用户代码可能需要重新编译
  • for micro-level releases, Boost.Python and user code rarely needs recompiled 对于微级版本,Boost.Python和用户代码很少需要重新编译

In all cases where Boost.Python needs to be recompiled, no other Boost library should need to be recompiled. 在需要重新编译Boost.Python的所有情况下,不需要重新编译其他Boost库。 When building multiple versions of Boost.Python, verify a clean build occurs. 构建Boost.Python的多个版本时,请验证是否发生了干净的构建。 Without a clean build, Boost.Python may build, but fail to properly link. 如果没有干净的构建,Boost.Python可能会构建,但无法正确链接。 For example, the PyClass_Type symbol should not be referenced in Python 3 Boost.Python builds, but without a clean build, previous build artifacts may populate the library: 例如, PyClass_Type符号不应在Python 3 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

Note that even though the 3noclean build was built against Python 3, the previous build that was using Python 2 had artifacts that polluted the 3noclean library. 请注意,尽管3noclean构建是针对Python 3构建的,但使用Python 2的先前构建具有污染3noclean库的工件。 Also, be aware that Boost.Python and user code may need recompiled to match the Python's CPU architecture and UCS-2 or UCS-4 unicode configurations. 另外,请注意Boost.Python和用户代码可能需要重新编译才能匹配Python的CPU架构和UCS-2或UCS-4 unicode配置。

As of Boost 1.67, you can build boost so it generates a separate version of libboost_python for each version of Python that you specify without having to clean or rebuild as part of the process. 从Boost 1.67开始,您可以构建boost,以便为您指定的每个Python版本生成单独版本的libboost_python,而无需在流程中清理或重建。

For instance, I edit tools/build/src/user-config.jam to contain the versions of Python on the system: 例如,我编辑tools/build/src/user-config.jam以包含系统上的Python版本:

using python : 2.7 : /opt/python/cp27-cp27mu/bin/python : /opt/python/cp27-cp27mu/include/python2.7 : /opt/python/cp27-cp27mu/lib ;
using python : 3.5 : /opt/python/cp35-cp35m/bin/python : /opt/python/cp35-cp35m/include/python3.5m : /opt/python/cp35-cp35m/lib ;
using python : 3.6 : /opt/python/cp36-cp36m/bin/python : /opt/python/cp36-cp36m/include/python3.6m : /opt/python/cp36-cp36m/lib ;
using python : 3.7 : /opt/python/cp37-cp37m/bin/python : /opt/python/cp37-cp37m/include/python3.7m : /opt/python/cp37-cp37m/lib ;

Then, I run ./b2 python=2.7,3.5,3.6,3.7 for the build step. 然后,我运行./b2 python=2.7,3.5,3.6,3.7进行构建步骤。

Some downstream packages expect the boost python libraries to be called libboost_python.so for Python 2.x and libboost_python3.so for Python 3.x, and you might need to explicitly specify -lboost_python27 , -lboost_python35 , etc. to get those programs to link properly. 一些下游软件包希望升压Python库被称为libboost_python.so为Python 2.x和libboost_python3.so为Python 3.x的,你可能需要明确指定-lboost_python27-lboost_python35等获得这些程序链接正常。

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

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