building.rst

来自「Boost provides free peer-reviewed portab」· RST 代码 · 共 681 行 · 第 1/2 页

RST
681
字号
.. Copyright David Abrahams 2006. Distributed under the Boost.. Software License, Version 1.0. (See accompanying.. file LICENSE_1_0.txt or copy at.. http://www.boost.org/LICENSE_1_0.txt)============================================== |(logo)|__ Boost.Python Build and Test HOWTO==============================================.. |(logo)| image:: ../../../boost.png   :alt: Boost C++ Libraries:   :class: boost-logo__ ../index.html.. section-numbering::   :depth: 2.. contents:: Contents   :depth: 2   :class: sidebar small.. |newer| replace:: *newer*Requirements============Boost.Python requires `Python 2.2`_ [#2.2]_ *or* |newer|__.  .. _Python 2.2: http://www.python.org/2.2__ http://www.python.orgBackground==========There are two basic models for combining C++ and Python:- extending_, in which the end-user launches the Python interpreter  executable and imports Python “extension modules” written in C++.  Think of taking a library written in C++ and giving it a Python  interface so Python programmers can use it.  From Python, these  modules look just like regular Python modules.- embedding_, in which the end-user launches a program written  in C++ that in turn invokes the Python interpreter as a library  subroutine.  Think of adding scriptability to an existing  application... _extending: http://www.python.org/doc/current/ext/intro.html.. _embedding: http://www.python.org/doc/current/ext/embedding.htmlThe key distinction between extending and embedding is the locationof the C++ ``main()`` function: in the Python interpreter executable,or in some other program, respectively.  Note that even whenembedding Python in another program, `extension modules are oftenthe best way to make C/C++ functionality accessible to Pythoncode`__, so the use of extension modules is really at the heart ofboth models.__ http://www.python.org/doc/current/ext/extending-with-embedding.htmlExcept in rare cases, extension modules are built asdynamically-loaded libraries with a single entry point, which meansyou can change them without rebuilding either the other extensionmodules or the executable containing ``main()``... _quickstart:No-Install Quickstart=====================There is no need to “install Boost” in order to get started usingBoost.Python.  These instructions use Boost.Build_ projects,which will build those binaries as soon as they're needed.  Yourfirst tests may take a little longer while you wait forBoost.Python to build, but doing things this way will save you fromworrying about build intricacies like which library binaries to usefor a specific compiler configuration and figuring out the rightcompiler options to use yourself... .. raw:: html   <div style="width:50%">.. Note:: Of course it's possible to use other build systems to   build Boost.Python and its extensions, but they are not   officially supported by Boost.  Moreover **99% of all “I can't   build Boost.Python” problems come from trying to use another   build system** without first following these instructions.   If you want to use another system anyway, we suggest that you   follow these instructions, and then invoke ``bjam`` with the   .. parsed-literal::     ``-a -o``\ *filename*    options to dump the build commands it executes to a file, so   you can see what your alternate build system needs to do... .. raw:: html    </div>.. _Boost.Build: ../../../tools/build/index.htmlBasic Procedure---------------1. Get Boost; see sections 1 and 2 [`Unix/Linux`__, `Windows`__\ ] of the   Boost `Getting Started Guide`_.   __ ../../../more/getting_started/unix-variants.html#get-boost   __ ../../../more/getting_started/windows.html#get-boost2. Get the ``bjam`` build driver.  See section 5 [`Unix/Linux`__,   `Windows`__\ ] of the Boost `Getting Started Guide`_.   __ ../../../more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary   __ ../../../more/getting_started/windows.html#prepare-to-use-a-boost-library-binary3. cd into the ``libs/python/example/quickstart/`` directory of your   Boost installation, which contains a small example project.4. Invoke ``bjam``.  Replace the “\ ``stage``\ “ argument from the   example invocation from section 5 of the `Getting Started   Guide`_ with “\ ``test``\ ,“ to build all the test targets.  Also add   the argument “\ ``--verbose-test``\ ” to see the output generated by   the tests when they are run.   On Windows, your ``bjam`` invocation might look something like:   .. parsed-literal::     C:\\boost_1_34_0\\…\\quickstart> **bjam toolset=msvc --verbose-test test**   and on Unix variants, perhaps,   .. parsed-literal::     ~/boost_1_34_0/…/quickstart$ **bjam toolset=gcc --verbose-test test**.. Admonition:: Note to Windows Users   For the sake of concision, the rest of this guide will use   unix-style forward slashes in pathnames instead of the   backslashes with which you may be more familiar.  The forward   slashes should work everywhere except in `Command Prompt`_   windows, where you should use backslashes.   .. _Command Prompt: ../../../more/getting_started/windows.html#command-promptIf you followed this procedure successfully, you will have built anextension module called ``extending`` and tested it by running aPython script called ``test_extending.py``.  You will also havebuilt and run a simple application called ``embedding`` that embedspython... _Getting Started Guide: ../../../more/getting_started/index.htmlIn Case of Trouble------------------If you're seeing lots of compiler and/or linker error messages,it's probably because Boost.Build is having trouble finding yourPython installation.  You might want to pass the``--debug-configuration`` option to ``bjam`` the first few timesyou invoke it, to make sure that Boost.Build is correctly locatingall the parts of your Python installation.  If it isn't, consider`Configuring Boost.Build`_ as detailed below.If you're still having trouble, Someone on one of the followingmailing lists may be able to help:* The `Boost.Build mailing list`__ for issues related to Boost.Build* The Python `C++ Sig`__ for issues specifically related to Boost.Python__ http://www.boost.org/more/mailing_lists.htm#jamboost__ http://www.boost.org/more/mailing_lists.htm#cplussigIn Case Everything Seemed to Work---------------------------------Rejoice!  If you're new to Boost.Python, at this point it might bea good idea to ignore build issues for a while and concentrate onlearning the library by going through the tutorial_ and perhapssome of the `reference documentation`_, trying out what you'velearned about the API by modifying the quickstart project... _reference documentation: v2/reference.html.. _tutorial: tutorial/index.htmlModifying the Example Project-----------------------------If you're content to keep your extension module forever in onesource file called |extending.cpp|_, inside your Boostdistribution, and import it forever as ``extending``, then you canstop here.  However, it's likely that you will want to make a fewchanges.  There are a few things you can do without having to learnBoost.Build_ in depth.The project you just built is specified in two files in the currentdirectory: |boost-build.jam|_, which tells ``bjam`` where it canfind the interpreted code of the Boost build system, and|Jamroot|_, which describes the targets you just built.  Thesefiles are heavily commented, so they should be easy to modify.Take care, however, to preserve whitespace.  Punctuation such as``;`` will not be recognized as intended by ``bjam`` if it is notsurrounded by whitespace... |boost-build.jam| replace:: ``boost-build.jam``.. _boost-build.jam: ../example/quickstart/boost-build.jam.. |Jamroot| replace:: ``Jamroot``.. _Jamroot: ../example/quickstart/Jamroot.. |extending.cpp| replace:: ``extending.cpp``.. _extending.cpp: ../example/quickstart/extending.cppRelocate the Project....................You'll probably want to copy this project elsewhere so you canchange it without modifying your Boost distribution.  To do that,simplya. copy the entire ``libs/python/example/quickstart/`` directory   into a new directory.b. In the new copies of |boost-build.jam|_ and |Jamroot|_, locate   the relative path near the top of the file that is clearly   marked by a comment, and edit that path so that it refers to the   same directory your Boost distribution as it referred to when   the file was in its original location in the   ``libs/python/example/quickstart/`` directory.For example, if you moved the project from``/home/dave/boost_1_34_0/libs/python/example/quickstart`` to``/home/dave/my-project``, you could change the first path in|boost-build.jam|_ from.. parsed-literal::  **../../../..**\ /tools/build/v2to.. parsed-literal::  **/home/dave/boost_1_34_0**\ /tools/build/v2and change the first path in |Jamroot|_ from.. parsed-literal::  **../../../..**to.. parsed-literal::  **/home/dave/boost_1_34_0**Add New or Change Names of Existing Source Files................................................The names of additional source files involved in building yourextension module or embedding application can be listed in|Jamroot|_ right alongside ``extending.cpp`` or ``embedding.cpp``respectively.  Just be sure to leave whitespace around eachfilename::  … file1.cpp file2.cpp file3.cpp …Naturally, if you want to change the name of a source file you cantell Boost.Build about it by editing the name in |Jamroot|_.Change the Name of your Extension Module........................................The name of the extension module is determined by two things:1. the name in |Jamroot|_ immediately following ``python-extension``, and 2. the name passed to ``BOOST_PYTHON_MODULE`` in |extending.cpp|_.To change the name of the extension module from ``extending`` to``hello``, you'd edit |Jamroot|_, changing.. parsed-literal::  python-extension **extending** : extending.cpp ;to.. parsed-literal::  python-extension **hello** : extending.cpp ;and you'd edit extending.cpp, changing.. parsed-literal::  BOOST_PYTHON_MODULE(\ **extending**\ )to.. parsed-literal::  BOOST_PYTHON_MODULE(\ **hello**\ )Installing Boost.Python on your System======================================Since Boost.Python is a separately-compiled (as opposed to`header-only`_) library, its user relies on the services of aBoost.Python library binary.  .. _header-only: ../../../more/getting_started/windows.html#header-only-librariesIf you need a regular installation of the Boost.Python librarybinaries on your system, the Boost `Getting Started Guide`_ willwalk you through the steps of creating one.  If building binariesfrom source, you might want to supply the ``--with-python``argument to ``bjam`` (or the ``--with-libraries=python`` argumentto ``configure``), so only the Boost.Python binary will be built,rather than all the Boost binaries.Configuring Boost.Build=======================As described in the `Boost.Build reference manual`__, a file called``user-config.jam`` in your home directory [#home-dir]_ is used tospecify the tools and libraries available to the build system. Youmay need to create or edit ``user-config.jam`` to tell Boost.Buildhow to invoke Python, ``#include`` its headers, and link with itslibraries.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?