Boost.Python Build and Test HOWTO

Boost.Python鹽蓫⢊ԈOWTO

1   Requirements

1   Ҫdz

Boost.Python requires Python 2.21 or newer.

Boost.PythonҪdzPython 2.21 벼/em> 輸߰漯em>᣼/p>

2   Background

2   ᳾༯a>

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.

שꏃ++ꍐythonӐ}֖빱ʽ㺼/p>

  • 㺍 ׮֕Ӄ맔ːython͆糌㬲⵼ȫӃC++᠐䵄Pythonᰀ久ģ視ᡣ ϫϱһ趃++兒⣬腳쒻趐ython퓿ڣ썊բѹPython㌐┬ɒԊ鈴˼ᣍ 䓐ython4ﴣ앢ģ奄͏ኇ㣹淨Pythonģ視㼯li>
  • ĚǶ㺍 ׮֕Ӄ맔ːЃ++儳̐⣬紹퀴巔Dython͆磬ᵷӃһ趿⺯ʽᣍ ϫϱһςΪϖӐ儓擃㌐⼓ȫ텱f᣼/li>

The key distinction between extending and embedding is the location of the C++ main() function: in the Python interpreter executable, or in some other program, respectively. Note that even when embedding Python in another program, extension modules are often the best way to make C/C++ functionality accessible to Python code, so the use of extension modules is really at the heart of both models.

)չꍄڇ淨阼쐔Ǹᰊǣ샫+ main() ꯊλփʇԚPython͆糌֐㬍 빊ǔچ䋻㌐▐㿠 עҢ㬼䊹ʇԚ㌐▐ĚǶPython㬍 )չģ祉鍹ʇPython亂뀻ӃC/C++餛ܗ儷튽㬍 ˹Ҕ㬕⁽֖ģʽʵ슉ϊǒԀ久ģ冀Ċ鈴Ϊ֐Є儡㍊

Except in rare cases, extension modules are built as dynamically-loaded libraries with a single entry point, which means you can change them without rebuilding either the other extension modules or the executable containing main().

㽁˼뉙ʽǩﶣ썊)չģ琢ǰ存Ӑ奒눫㵄毌쿢鹽資㬍 բҢζׅ㬍 ģԐ޸ċ샇㬍 渲뱘֘䋻)չģ祉 벰캬 main() 監䐐㌐⡣

3   No-Install Quickstart

3   aಗ࿬˙ȫA

There is no need to "install Boost" in order to get started using Boost.Python. These instructions use Boost.Build projects, which will build those binaries as soon as they're needed. Your first tests may take a little longer while you wait for Boost.Python to build, but doing things this way will save you from worrying about build intricacies like which library binaries to use for a specific compiler configuration and figuring out the right compiler options to use yourself.

ʹӃBoost.Python⻐蒪ᰰ◰Boostᱡ㍊բ/儽̳̀듃K Boost.Build ϮĿ㬍 ˼롔ڐ蒪ʱה毹齨ć澽薆㍊ģ儵ڒ븶⢊ԣ쿉ĜҪ폳䵄ʱ줣썊ģҪ剁킯ost.Python儹齨㬍 嫕6׶㬈A㲻ᘵ㓇䭗۸䔓儹齨㬍 ȧ㬄㸶̘櫻ҫƷŤփ胊鈴Ą趶ֆ썊ҲȃģaӚה캉薃᠒놷ѡϮᣍ

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.

屈룬ҲԊ鈴Ƥ˻儱ҫϵͳ㬍 4鹽肯ost.Python찆䀩չ㬍 嫋샇⻊ǂoostսʽ֧㖵ġ㍊˹ӐᰎҎ޷狙ҫBoost.PythonᱵĎʌ⣬ 99%ҔɏʇҲΪʔͼʹӃmһ֖᠒돵ͳ㬍 渃듐ψക⸶팳̗檔

If you want to use another system anyway, we suggest that you follow these instructions, and then invoke bjam with the

-a -ofilename

options to dump the build commands it executes to a file, so you can see what your alternate build system needs to do.

ȧ黃㏫ʹӃmһ趏即㬍 ΒC鄣ψױѭբָʾ㬍 Ȼ곓Oԏ2ΊӃ bjam

-a -ofilename

4ʤ㶋추΄쾖䐐儹齨un㬍 բѹģɒԿ䵽ģ儱ҫϵͳҪ׶ʲoᣍ

3.1   Basic Procedure

3.1   빱㌼/a>

  1. Get Boost; see sections 1 and 2 [Unix/Linux, Windows] of the Boost Getting Started Guide.

    뱈Ⴏost㻍 컂oostȫAָď 戲ꍵڲ횛Unix/LinuxWindows]ᣍ

  2. Get the bjam build driver. See section 5 [Unix/Linux, Windows] of the Boost Getting Started Guide.

    뱈ἴt class="docutils literal">bjam鹽陷毆硣 컂oost ȫAָA 嚵횛Unix/LinuxWindows]᣼/p>

  3. cd into the libs/python/example/quickstart/ directory of your Boost installation, which contains a small example project.

    cd發oostಗĿ¼ς儼tt class="docutils literal">libs/python/example/quickstart/㬍 ć/Ӑ趐ᵄʾ=餳̡㼯p>

  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.

    巔utt class="docutils literal">bjamᣍ a class="reference external" href="../../../more/getting_started/index.html">ȫAָď 嚵횀튾儵瓃un֐傻 ᰼tt class="docutils literal">stageᱲΊ퍊̦뻎ꡰtestᱣ썊4鹽蘋Ӑ⢊Ԅﱪᣠ ͬʱ̭쓲Ί퍊᰼tt class="docutils literal">--verbose-testᱍ 4⩿䲢ʔԋʱɺ㉵Ċ䳶᣼/p>

    On Windows, your bjam invocation might look something like:

    ԚWindowsɏ㬄㵄 bjam 巔yɄ܏ᕢѹ㺍

    C:\boost_1_34_0\᭜quickstart> bjam toolset=msvc --verbose-test test
    

    and on Unix variants, perhaps,

    漁ڸ癤Unixɏ㬿Ʉ܊ǣ꼯p>

    ~/boost_1_34_0/᭯quickstart$ bjam toolset=gcc --verbose-test test
    

Note to Windows Users

WindowsӃ맗⒢

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.

Ϊdz첽࣬ᾖ髒儆䓠⿷ֽ딚·Њ鈴unix秸ᵄ蜣썊渲늇ģܸ슬Ϥ儷䐱蠟㍊蜓渃彴濉Ӄ㬳큋 un̡ʾ綜/a> 䰿ڣ씚ć/ĺӦ胊鈴紐Ḝᣍ

If you followed this procedure successfully, you will have built an extension module called extending and tested it by running a Python script called test_extending.py. You will also have built and run a simple application called embedding that embeds python.

ȧ黃ㄜ㉹浘ױѭբ趹㬍 ģ齨㶒븶)չģ謹썊uΪextending㬍 ⢔ː븶uΪ test_extending.py 儐ython텱ʔ˼ᣍ ģҲ齨⢔ː븶ĚǶpython儼ⵥӦӃ㌐⣬ uΪembedding

3.2   In Case of Trouble

3.2   ȧ黓浽Ί̢

If you're seeing lots of compiler and/or linker error messages, it's probably because Boost.Build is having trouble finding your Python installation. You might want to pass the --debug-configuration option to bjam the first few times you invoke it, to make sure that Boost.Build is correctly locating all the parts of your Python installation. If it isn't, consider Configuring Boost.Build as detailed below.

ȧ黃㼻彴᠒뻲l퓵Ĵ펳ЅϢ㬍 ć܊ǒ⎪Boost.BuildΞ稕ҵ턣儐ythonಗࡣ ģҲҪԚ׮㵼贎巔utt class="docutils literal">bjamʱ㬍 䫈뼴t class="docutils literal">--debug-configuration⎊ Ҕȷᣂoost.Buildսȷ樎끋˹ӐPythonש쾡㠍 ȧ黲늇㬇뿼‡ ŤփBoost.Build

If you're still having trouble, Someone on one of the following mailing lists may be able to help:

ȧ黈ԓЎʌ⣬ǫϲҔςӊ쾁푰dz௖꣺

3.4   Modifying the Example Project

3.4   脊餳̼/a>

If you're content to keep your extension module forever in one source file called extending.cpp, inside your Boost distribution, and import it forever as extending, then you can stop here. However, it's likely that you will want to make a few changes. There are a few things you can do without having to learn Boost.Build in depth.

ȧ黃ףӚ䋣썊)չģ冀Ĕ䎄쾓抇 extending.cpp㬍 λӚBoost碲섿¼֐㬍 ⢓水extending弈룬 ćoģɒԵΪֹᣠ ⻹܊ǣ섣롏뗷һᤶ Ӑ櫳„㿉Ҕ׶㬶莞ɮȫѧϰ Boost.Build

The project you just built is specified in two files in the current directory: boost-build.jam, which tells bjam where it can find the interpreted code of the Boost build system, and Jamroot, which describes the targets you just built. These files 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 not surrounded by whitespace.

ģ蕸չ齨儹䳌㬍 ʇӉ屇Ŀ¼ς俊΄쾏ꏸ˵÷廣ꍊboost-build.jam 㨋츦˟bjam㬍 Ą/ԕҵ킯ost᠒돵ͳ儽⊍亂룩㬍 ꍼa class="reference external" href="../example/quickstart/Jamroot">Jamroot 㨋샨ʶKģ蕸չ齨億ﱪ㩡㍊բ΄쾾ߓЏꏸ儗⊍㬋鍆˼CӦ胺܈ݒא޸ġ㍊⻹퐡Є㬒걣tחַ롣 ȧ;բѹ儱굣綹ţ썊ȧ黖܎烻Ӑחַ룬 bjam޷芶ᰡ㍊

Relocate the Project

餳̒Ǝ뼯h3>

You'll probably want to copy this project elsewhere so you can change it without modifying your Boost distribution. To do that, simply

ģ܏밑胹䳌贖Ƶ톤˻λփ㬍 բѹģɒԐ޸ċ죬 渲뱘ԚBoost碲섿¼֐輸ġ㍊Ҫ׶錄Ⓕ壣얻

  1. copy the entire libs/python/example/quickstart/ directory into a new directory.
  2. 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.
  1. 贖ƕ븶libs/python/example/quickstart/ Ŀ¼歸븶Ђ億ᣍ
  2. ԚЂ儼a class="reference external" href="../example/quickstart/boost-build.jam">boost-build.jam ꍼa class="reference external" href="../example/quickstart/Jamroot">Jamroot 豱㬍 �쎄쾶巋㬍 Ւ當ɒ댵עʍǥ㾱ꗢ粲ඔ·썊Ȼ곱༭բ趁羶㬍 ȃ˼ָϲBoost碲섿¼㬍 ӫ˼ԭ4㨵ᎄ쾔ڼtt class="docutils literal">libs/python/example/quickstart/ Ŀ¼ʱ㩋壼Ŀ¼Ϡͬᣍ

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

=ȧ㬈繻ģ儹䳌䓍 /home/dave/boost_1_34_0/libs/python/example/quickstart ҆ց/home/dave/my-project㬍 ģԸ츄 boost-build.jam ֐儵ڒ븶·촓

../../../../tools/build/v2

to

脎꼯p>

/home/dave/boost_1_34_0/tools/build/v2

and change the first path in Jamroot from

⢇ң콫Jamroot ֐儵ڒ븶·Ӎ

../../../..

to

脎꼯p>

/home/dave/boost_1_34_0

Add New or Change Names of Existing Source Files

̭쓐μĔ䎄쾻⸼臠䎄쾃뼯h3>

The names of additional source files involved in building your extension module or embedding application can be listed in Jamroot right alongside extending.cpp or embedding.cpp respectively. Just be sure to leave whitespace around each filename:

鹽耩չģ嗢ℚǶӦӃ㌐⊱㬍 ɦ찵Ć䋻Դ΄쾵ă뗖㬍 Ԕڼa class="reference external" href="../example/quickstart/Jamroot">Jamroot֐P㶣썊皰PӚextending.cpp 벼tt class="docutils literal">embedding.cppŔ១㍊ֻʇy趎ļ֜Χ㬎᱘tςחַ룺

᭠file1.cpp file2.cpp file3.cpp ᭼/pre>

Naturally, if you want to change the name of a source file you can tell Boost.Build about it by editing the name in Jamroot.

屈룬ȧ黃㏫Ҫ輸Ĕ䎄쾃룬 ģԱ༭ Jamroot ֐載ļ㬀並˟Boost.Buildᣍ

Change the Name of your Extension Module

輸Ā久ģ冀ă뗖

The name of the extension module is determined by two things:

)չģ冀ă뗖㬓Ɂ嗥樣꼯p>

  1. the name in Jamroot immediately following python-extension, and
  2. the name passed to BOOST_PYTHON_MODULE in extending.cpp.
  1. ԚJamroot ֐꼴t class="docutils literal">python-extension 儃뗖㬍 ꍼ/li>
  2. Ԛextending.cpp֐ 䫸輴t class="docutils literal">BOOST_PYTHON_MODULE 儃뗖᣼/li>

To change the name of the extension module from extending to hello, you'd edit Jamroot, changing

Ҫ久ģ暑봓extending 脎꼴t class="docutils literal">hello㬍 ģҪᠼ class="reference external" href="../example/quickstart/Jamroot">Jamroot㬽덊

python-extension extending : extending.cpp ;

to

脎꼯p>

python-extension hello : extending.cpp ;

and you'd edit extending.cpp, changing

빐蒪ᠼtending.cpp㬽뼯p>

BOOST_PYTHON_MODULE(extending)

to

脎꼯p>

BOOST_PYTHON_MODULE(hello)

4   Installing Boost.Python on your System

4   Ԛϵͳɏಗ¯ost.Python

Since Boost.Python is a separately-compiled (as opposed to header-only) library, its user relies on the services of a Boost.Python library binary.

ӉӚBoost.Pythonʇһ趵嶀᠒뵄蓐ᰓڼa class="reference external" href="../../../more/getting_started/windows.html#header-only-libraries">俍玄쾼/a>養 ˼儓u琨ҪҀ5Boost.Python澽薆ķᣍ

If you need a regular installation of the Boost.Python library binaries on your system, the Boost Getting Started Guide will walk you through the steps of creating one. If building binaries from source, you might want to supply the --with-python argument to bjam (or the --with-libraries=python argument to configure), so only the Boost.Python binary will be built, rather than all the Boost binaries.

ȧ黃㐨ҪԚϵͳɏಗһ足㹦儂oost.Python澽薆썊BoostȫAָď脣һ⽒벽䴽笙ᣍ ȧ黴Ӕ䎄쾹齨澽薆㬍 ģҲҪϲbjam ̡駝tt class="docutils literal">--with-python ⎊벏⼴t class="docutils literal">configure ̡駝tt class="docutils literal">--with-libraries=python ⎊㬍 բѹὶ齨Boost.Python澽薆㬍 渲늇˹Ӑ儂oost澽薆᣼/p>

5   Configuring Boost.Build

5   ŤփBoost.Build

As described in the Boost.Build reference manual, a file called user-config.jam in your home directory6 is used to specify the tools and libraries available to the build system. You may need to create or edit user-config.jam to tell Boost.Build how to invoke Python, #include its headers, and link with its libraries.

սȧBoost.Build⎿슖⡼/a>˹ʶ㬍 Ԛģ監焿¼6ς㬍 Ӑһ趼tt class="docutils literal">user-config.jam΄쾣썊Ӄ4ָ櫻ҫϵͳoĹ存ꍿ⡣ ĺܐ蒪䴽軲ᠼt class="docutils literal">user-config.jam㬍 4見߂oost.Buildȧꎵ瓃Python㬍 #include ˼傻玄쾣첢t퓋쵄㍊

Users of Unix-Variant OSes

Unix OSӃ맼/p>

If you are using a unix-variant OS and you ran Boost's configure script, it may have generated a user-config.jam for you.4 If your configure/make sequence was successful and Boost.Python binaries were built, your user-config.jam file is probably already correct.

ȧ黃㊹Ӄ儊ǀծix♗珵ͳ㬍 ⢇҄㔋KBoost configure 텱 ˼ܒѾ펪ģɺ休˒븶user-config.jam4ᣍ ȧ黃㼴t class="docutils literal">configure/make ܳɹ湹oost.Python㬍 ģ儼tt class="docutils literal">user-config.jam ΄쾿ɄܒѾ튇սȷ俊ˡ㍊

If you have one fairly "standard" python installation for your platform, you might not need to do anything special to describe it. If you haven't configured python in user-config.jam (and you don't specify --without-python on the Boost.Build command line), Boost.Build will automatically execute the equivalent of

ȧ黃㵄ƽ̨Ӑһ趏൱ᰱꗼᱵİythonಗ࣬ ģܲ됨Ҫ攋엶̘ᰃ芶ᣍ ȧ黃・ӐԚuser-config.jam ֐Ťփpython㨲⇒uӐԚBoost.Buildun֐ָ樍 --without-python㩣썊Boost.BuildԶҔς刐甌

import toolset : using ;
using python ;

which automatically looks for Python in the most likely places. However, that only happens when using the Boost.Python project file (e.g. when referred to by another project as in the quickstart method). If instead you are linking against separately-compiled Boost.Python binaries, you should set up a user-config.jam file with at least the minimal incantation above.

բ롈E씚׮Ӑܵĵط헔毑ՒPythonᣠ ⻹բֻ롷≺Ԛ屆鈴Boost.PythonϮĿ΄쾊ፊ㨀툧㬵ᱻmһ趏ҽӃʱ㬈獊﬋و냅˹ʾ罷褌ᣍ 紖ȧ黃퓵嶀᠒뵄Boost.Python澽薆㬍 ģӦ能聢һ趍 user-config.jam΄쾣썊ցə༺쒔ɏ׮ə監䓯ᣍ

5.1   Python Configuration Parameters

5.1   PythonŤփ⎊a>

If you have several versions of Python installed, or Python is installed in an unusual way, you may want to supply any or all of the following optional parameters to using python.

ȧ黃㰲װK核搹thon㬍 벐ythonҔһ趲둰㣵ķ튽ಗ࣬ ģܒꏲusing python ̡驏﷖번벿᲎ʽᣍ

version
the version of Python to use. Should be in Major.Minor format, for example, 2.3. Do not include the subminor version (i.e. not 2.5.1). If you have multiple Python versions installed, the version will usually be the only configuration argument required.

ʹӃ儐ython儰汾ᣍ 豊펪ᰖ璪঱.䎒갦Ὰšᣬ =ȧ2.3ᣍ ⻰쀨ד䎰汾ꅣ輴⻊Ǽ/em>2.5.1㩡㍊ȧ黃㰲װK核氦᾵Đython㬍 ঱ͨ㣊ǎ蒻Ҫdz儅䖃⎊

cmd-or-prefix
preferably, a command that invokes a Python interpreter. Alternatively, the installation prefix for Python libraries and header files. Only use the alternative formulation if there is no appropriate Python executable available.

׮ꃊǒ븶巔Dython͆組unᣍ ҲԊǣ쐹thon͍玄쾵İ◰ǰ׺ᣍ ჻ӐꏊʵĐython䐐㌐⊱㬍 ⅊鈴嚶罊

includes
the #include paths for Python headers. Normally the correct path(s) will be automatically deduced from version and/or cmd-or-prefix.

Pythonͷ΄쾵ļtt class="docutils literal">#include·㍊ͨ㣣약ȷ儂羶Ӎ versioncmd-or-prefix ה母ƶϡ㍊

libraries
the path to Python library binaries. On MacOS/Darwin, you can also pass the path of the Python framework. Normally the correct path(s) will be automatically deduced from version and/or cmd-or-prefix.

Python澽薆Ă羶ᣍ ԚMacOS/Darwinɏ㬄㻹Դ눫PythonﲼܵĂ羶ᣍ ͨ㣣약ȷ儂羶Ӎ versioncmd-or-prefix ה母ƶϡ㍊

condition
if specified, should be a set of Boost.Build properties that are matched against the build configuration when Boost.Build selects a Python configuration to use. See examples below for details.

ȧ黑ꖸ樣쓦胊ǒ뗩Boost.Buildʴ㬍 層oost.BuildѡԱʹӃһ趐ythonŤփʱ㬍 ӃӚ臍䄬ȏ儹齨Ťփᣍ Ϫǩ컏ƒ怽דᣍ

extension-suffix
A string to append to the name of extension modules before the true filename extension. You almost certainly don't need to use this. Usually this suffix is only used when targeting a Windows debug build of Python, and will be set automatically for you based on the value of the <python-debugging> feature. However, at least one Linux distribution (Ubuntu Feisty Fawn) has a specially configured python-dbg package that claims to use such a suffix.

һ趗ַ봮㬓A麗쓵퀩չģ暑뉏㨔ڕ抵載ļչu֮ǰ㩡㍊ģ츺懺樲됨ҪӃ錄⸶ᣍ ͨ㣕⸶곗ꖻӃӚի攗indows儐ython嬰԰棬 ⢇ң싼롻銍 <python-debugging> ̘監嗔毉趨ᣍ ⻹ցəӐһ趌inux堎棨Ubuntu Feisty Fawn㩣썊Ӑһ趌رŤփ傻 python-dbg ༣썊ɹㆊ鈴Kբѹ儺㗺ᣍ

5.2   Examples

5.2   =ד

Note that in the examples below, case and especially whitespace are significant.

עҢ㬔ڏƒ淨=ד֐㬴㐡㬼em>ӈƤʇחַ뼯em>ʇӐҢҥ儡㍊

  • If you have both python 2.5 and python 2.4 installed, user-config.jam might contain:

    ȧ黃㍬ʱಗKpython 2.5ꍰython 2.4㬍 user-config.jamܰ캬㺍

    using python : 2.5 ;  # Make both versions of Python available
    
    using python : 2.4 ;  # To build with python 2.4, add python=2.4
                          # to your command line.
    

    The first version configured (2.5) becomes the default. To build against python 2.4, add python=2.4 to the bjam command line.

    麼븶Ťփ儰汾㨲.5㩽볉ΪĬȏֵᣍ ȧ黑ꊹӃpython 2.4鹽裬 ǫԚbjamun֐̭쓍 python=2.4

  • If you have python installed in an unusual location, you might supply the path to the interpreter in the cmd-or-prefix parameter:

    ȧ黃㰑pythonಗԚһ趲둰㣵Ď떃㬍 ģԔڼtt class="docutils literal">cmd-or-prefix⎊햐 ̡駑⊍Ʒ儂羶㺍

    using python : : /usr/local/python-2.6-beta/bin/python ;
    
  • If you have a separate build of Python for use with a particular toolset, you might supply that toolset in the condition parameter:

    ȧ黑븶̘樹存쯊鈴Kһ趵嶀鹽資Python㬍 ģԔڼtt class="docutils literal">condition ⎊햐̡駙u存쯡㍊

    using python ;  # use for most toolsets
    
    # Use with Intel C++ toolset
    using python
         : # version
         : c:\\Devel\\Python-2.5-IntelBuild\\PCBuild\\python # cmd-or-prefix
         : # includes
         : # libraries
         : <toolset>intel # condition
         ;
    
  • If you have downloaded the Python sources and built both the normal and the "python debugging" builds from source on Windows, you might see:

    ȧ黃㏂ԘKPythonԴ«⢇Ҕڗindowsɏה캹齨Kpythonꍍ python嬰԰漯a>㬍 ģܻῴ彣ꍊ

    using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python ;
    using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python_d
      : # includes
      : # libs
      : <python-debugging>on ;
    
  • You can set up your user-config.jam so a bjam built under Windows can build/test both Windows and Cygwin python extensions. Just pass <target-os>cygwin in the condition parameter for the cygwin python installation:

    ģԉ薃user-config.jam㬍 ȃWindowsɏ儢jamĜ黹齨ꍲ⊔}躎汾儰ython)չ㬍 숓indowsণ썊ҲӐCygwinড㍊ֻԚcondition⎊햐㬍 Ϊcygwin儰ythonಗ͊䫈뼴t class="docutils literal"><target-os>cygwin

    # windows installation
    using python ;
    
    # cygwin installation
    using python : : c:\\cygwin\\bin\\python2.5 : : : <target-os>cygwin ;
    

    when you put target-os=cygwin in your build request, it should build with the cygwin version of python:5

    屄㔚鹽臫dz֐쓈봡rget-os=cygwin㬍 ˼渃ʹӃcygwin঱python齨㺼a class="footnote-reference" href="#flavor" id="id15">5

    bjam target-os=cygwin toolset=gcc

    This is supposed to work the other way, too (targeting windows python with a Cygwin bjam) but it seems as though the support in Boost.Build's toolsets for building that way is broken at the time of this writing.

    mһ֖罊퓦胒␐ 㨊鈴Cygwinɏ儢jam㬍 Ҕwindows pythonΪĿ᪣養 嬪ڱ׷֮ʱ㬍 Boost.Build儹存쯶Ԅǖֹ齨罊֧㖺IᓐΊ̢ᣍ

  • Note that because of the way Boost.Build currently selects target alternatives, you might have be very explicit in your build requests. For example, given:

    עҢ㬔ڍ Boost.BuildĿǰѡԱ⻍섿᪵ķ튽ς㬍 ܄㵄᠒뇫dzᘐ뷇㣃爺ᣍ 퀴˵㬈瓐㺍

    using python : 2.5 ; # a regular windows build
    using python : 2.4 : : : : <target-os>cygwin ;
    

    building with

    Ҕ

    bjam target-os=cygwin
    

    will yield an error. Instead, you'll need to write:

    鹽軡⺉꒻趴펳ᣄ㐨Ҫբѹ⅐꼯p>

    bjam target-os=cygwin/python=2.4
    

6   Choosing a Boost.Python Library Binary

6   ѡԱBoost.Python澽薆/a>

If᪩nstead of letting Boost.Build construct and link with the right libraries automatically᪹ou choose to use a pre-built Boost.Python library, you'll need to think about which one to link with. The Boost.Python binary comes in both static and dynamic flavors. Take care to choose the right flavor for your application.2

ȧ黲늇ȎӉBoost.Buildה毹齨ꍁ體սȷ兒⣬ 淵ǑᔱʹӃԤĂoost.Python썊ģҪ〞ǒ돂t퓄ĸ濢ᣍ Boost.Python澽薆슱⌬ꍶ㍊ǫΪĺ儓擃ЄѡԱսȷ兒⡣2

6.1   The Dynamic Binary

6.1   毌춾Ƽ/a>

The dynamic library is the safest and most-versatile choice:

毌쿢ʇ׮ಈ뺍׮ͨӃ儑ᔱ㺼/p>

  • Ӄ̘樹存쯹齨儋銬)չģ謹썊漊鈴ͬһ睿ⴺ«᣼a class="footnote-reference" href="#toolset-specific" id="id18">3
  • 캬һ趀Ѝת뻗ⲡ᭡㍊ҲΪ˹Ӑ)չģ啕⏭һ趗ⲡ᭣싹Ҕ㬍 Ԛһ趶װԘ儀久ģ節썊弳浽Python儀ʵ=㬍 Դ븸mһ趶ģ節쳶儺ᣍ

6.2   The Static Binary

6.2   춾Ƽ/a>

It might be appropriate to use the static Boost.Python library in any of the following cases:

ςPȎһǩﶏ£쿉Ĝʊꏊ鈴쵄Boost.Pythonꍊ

  • You are extending python and the types exposed in your dynamically-loaded extension module don't need to be used by any other Boost.Python extension modules, and you don't care if the core library code is duplicated among them.
  • You are embedding python in your application and either:
    • You are targeting a Unix variant OS other than MacOS or AIX, where the dynamically-loaded extension modules can "see" the Boost.Python library symbols that are part of the executable.
    • Or, you have statically linked some Boost.Python extension modules into your application and you don't care if any dynamically-loaded Boost.Python extension modules are able to use the types exposed by your statically-linked extension modules (and vice-versa).
  • ģʇԚPython㬍 ⢇ң섣毌켓Ԙ儀久ģ節쳶儀Ѝ㬍 ⻐蒪Ệ䋻Boost.Python)չģ琢鈴㬍 ģҲ⻹ؐĺːĿⴺ«ʇ籔ڋ샇֐줖ظ䡣
  • ģʇԚӦӃ㌐▐ĚǶ Python㬲⇒㺍
    • ģ億ﱪʇMacOS벁IXҔ͢儀ծix OS㬍 Ԛć/㬶쓔صĀ久ģ謹썊ԡ࿴컡Έִ΄쾖Ђoost.Pythonķ뺅ᣍ
    • 법ߣ섣ґ⌬t퓁˒됩Boost.Python)չģ冀턣儓擃㌐⣬ ⢇҄㲻闡č 毌켓Ԙ儂oost.Python)չģ謹썊ʇ簣ܹ늹Ӄģ쁴퓵Ā久ģ碑鵼㶵ĀЍ㨷䖮ҠȻ㩡㍊

7   #include Issues

7   #includeΊ̢

  1. If you should ever have occasion to #include "python.h" directly in a translation unit of a program using Boost.Python, use #include "boost/python/detail/wrap_python.hpp" instead. It handles several issues necessary for use with Boost.Python, one of which is mentioned in the next section.
  2. Be sure not to #include any system headers before wrap_python.hpp. This restriction is actually imposed by Python, or more properly, by Python's interaction with your operating system. See http://docs.python.org/ext/simpleExample.html for details.
  1. ԚʹӃBoost.Python儳̐▐㬍 ȧ黃㐨ҪԚij趷풫奔ꖐֱ퓍 #include "python.h"㬍 ǫ脎ꊹӃ#include "boost/python/detail/wrap_python.hpp"ᣍ ˼䦀큋Boost.PythonʹӃ֐츸汘ҪΊ̢㬍 Ƥ֐һ趽딚퓏€䵄һ횖Ќᵽᣍ
  2. Ԛwrap_python.hpp֮ǰ㬍 ǧͲ⻒꼴t class="docutils literal">#include Ȏꎏ即ͷ΄쾡㍊բһϞֆʵ슉ϊǓɐythonǿ쓵ģ썊輑ϸᵘ˵㬍 ʇӉPythonӫģ儲ٗ珵ͳϠ륗瓃Ԭ㉵ġ㍊Ϫ컼a class="reference external" href="http://docs.python.org/ext/simpleExample.html">http://docs.python.org/ext/simpleExample.htmlᣍ

8   Python Debugging Builds

8   Python嬰԰漯a>

Python can be built in a special "python debugging" configuration that adds extra checks and instrumentation that can be very useful for developers of extension modules. The data structures used by the debugging configuration contain additional members, so a Python executable built with python debugging enabled cannot be used with an extension module or library compiled without it, and vice-versa.

PythonԓO븶̘ʢ儅䖃㬡హthon嬰ԡᣬ齨㬍 ˼Զ쓁˶儼첩ꍲ⊔㬍 攀久ģ冀Ŀ귢՟照㓐Ӄᣍ 嬰ԅ䖃˹ʹӃ儊養 ༺쁋軾ӵijɔᣬ Ҳ䋣썊Ӄpython嬰Ԅ㊽鹽資Python䐐㌐⣬ ⻄܊鈴燵犔ģʽ᠒뵄)չģ嗢⿢㬍 紖Ȼ᣼/strong>

Since pre-built "python debugging" versions of the Python executable and libraries are not supplied with most distributions of Python,7 and we didn't want to force our users to build them, Boost.Build does not automatically enable python debugging in its debug build variant (which is the default). Instead there is a special build property called python-debugging that, when used as a build property, will define the right preprocessor symbols and select the right libraries to link with.

ӉӚԚ䳶ʽPython堎斐㬍 uӐ̡驔包儡హthon嬰ԡᰦ儐ython䐐㌐⺍ 7㬍 渴Ҏ҃Dz돫ǿƈΒC儓u燭鹽笙C㬍 ˹Ҕ㬔ڂoost.Python儼tt class="docutils literal">debugখЍ 㨄숏ʇdebugণ養 uӐה屄⿪python嬰Ԅ㊽㬍 ׷Ϊ̦大쓐һ趌رൄ鹽芴㬍 ㆎ꼴t class="docutils literal">python-debugging㬍 ʹӃ胹齨ʴʱ㬍 롶蒥սȷ儔䴦-綹ţ첢ѡԱսȷ兒⽸t퓡㍊

On unix-variant platforms, the debugging versions of Python's data structures will only be used if the symbol Py_DEBUG is defined. On many windows compilers, when extension modules are built with the preprocessor symbol _DEBUG, Python defaults to force linking with a special debugging version of the Python DLL. Since that symbol is very commonly used even when Python is not present, Boost.Python temporarily undefines _DEBUG when Python.h is #included from boost/python/detail/wrap_python.hpp - unless BOOST_DEBUG_PYTHON is defined. The upshot is that if you want "python debugging" and you aren't using Boost.Build, you should make sure BOOST_DEBUG_PYTHON is defined, or python debugging will be suppressed.

Ԛ Unixƽ̨ɏ㬍 ᶨҥKPy_DEBUG綹Ŋᣬ ⅻኹӃ嬰԰搹thon儊顣 Ԛ栗indows᠒놷ɏ㬍 局久ģ禎O䴦-綹ō _DEBUG 鹽花㬍 PythonĬȏ롇t퓒븶̘ʢ廣쵷ʔ঱Python DLLᣍ ӉӚբ趷뺅ꜳ㓃㨉喁uӐPythonʱ㩣싹Ҕ㬍 Ԛboost/python/detail/wrap_python.hpp֐༺쐹thon.hʱ㬍 Boost.PythonԝʱȡϻK_DEBUG樒半᪡곽焦蒥KBOOST_DEBUG_PYTHONᣍ Ƥ늇㬈繻ģϫҪᰰython嬰ԡᣬ ⢇҄・ӐӃBoost.Build㬍 ģαᘶ蒥BOOST_DEBUG_PYTHON㬍 ⻈밹thon嬰Իᱻa㍊

9   Testing Boost.Python

9   ⢊Ԃoost.Python

To run the full test suite for Boost.Python, invoke bjam in the libs/python/test subdirectory of your Boost distribution.

ԚBoost儼tt class="docutils literal"> libs/python/testדĿ¼ς㬠 巔utt class="docutils literal">bjam㬍 ҔԋBoost.Pythonͪջ儲⊔༡㍊

10   Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users

10   MinGW㨼ugwin -mno-cygwin㩉χCCӃ맗⒢ʂϮ

If you are using a version of Python prior to 2.4.1 with a MinGW prior to 3.0.0 (with binutils-2.13.90-20030111-1), you will need to create a MinGW-compatible version of the Python library; the one shipped with Python will only work with a Microsoft-compatible linker. Follow the instructions in the "Non-Microsoft" section of the "building Extensions: Tips And Tricks" chapter in Installing Python Modules to create libpythonXX.a, where XX corresponds to the major and minor version numbers of your Python installation.

ȧ黃㕽ԚʹӃPython 2.4.1㬍 MinGW 3.0.0㨺͢inutils-2.13.90-20030111-1㩖儰汾㬍 ĺ蒪䴽蒻趓덩nGW즈ݵĐython덊Python堎Ћ洸兒▻ĜӃӚ΢ȭ즈ݵā콓Ʒᣍ ǫת彼a class="reference external" href="http://www.python.org/doc/current/inst/index.html">ಗthonģ塚/a>㬍 ᰹齨)չ㺌ኾꍼ쇉᱒땂㬍 ᰷ǎ∭᱒뽚㬍 കՖ芾4䴽輴t class="docutils literal">libpythonXX.a㬍 Ƥ֐XX Ϊģ儐ythonಗൄַҪꍴΒ갦Ὰš㍊


[1]Note that although we tested earlier versions of Boost.Python with Python 2.2, and we don't think we've done anything to break compatibility, this release of Boost.Python may not have been tested with versions of Python earlier than 2.4, so we're not 100% sure that python 2.2 and 2.3 are supported.

עҢ㬾ṜΒCԚPython 2.2ɏ⢊Թ킯ost.Python儔熚঱ ⢇Ҏ҃Ǽem>ȏΪΒCuӐƆ뵼賊㬍 嬙堎Ăoost.Python܃듐ԚPython 2.4Ҕǰ儰汾ɏ⢊Թ ˹ҔΒC⻊DZ00%兒϶脜֧㖰ython 2.2ꍲ.3ᣍ

[2]

Information about how to identify the static and dynamic builds of Boost.Python:

Ӑ閾纖ȷ樂oost.Python齉⌬঺Ͷণꍊ

[3]Because of the way most *nix platforms share symbols among dynamically-loaded objects, I'm not certain that extension modules built with different compiler toolsets will always use different copies of the Boost.Python library when loaded into the same Python instance. Not using different libraries could be a good thing if the compilers have compatible ABIs, because extension modules built with the two libraries would be interoperable. Otherwise, it could spell disaster, since an extension module and the Boost.Python library would have different ideas of such things as class layout. I would appreciate someone doing the experiment to find out what happens.

Ԛ䳶ʽ*nixƽ̨ɏ㬍 Ԛ毌연Ԙ攜ԏ㖮줣썊ӉӚ綹Ź⏭罊閼壬 Ӄ⻍챠ҫ餾߹齨儀久ģ謹썊屼Ӕص퍬һPythonʵ=ʱ㬍 Β⻄ܿ϶裬 ʇ籗܊NJ鈴⻍쵄Boost.Python㍊ ȧ黛ҫƷ漾ߓ賊俊BI㬍 ćo㬊鈴Ϡͬ兒⽫ʇһ쾺E£썊ҲΪӃ}趿⹹ā)չģ墳뿉Ҕ륲ٗ硣 籔⽫一䔖đ㬍 ҲΪ)չģ喙͂oost.Python썊Ԉ瀠⼾֖儊‡養 Ӑ⻍쵄ϫ稡㍊ ȧ黓Ј˗抵ѩ㬒ԁ˽⻡碉ꊲo㬎ҽ벻ʤ萼䡣

[4]configure overwrites the existing user-config.jam in your home directory (if any) after making a backup of the old version.

configure롸⸇ַĿ¼ςϖӐ傻 user-config.jam㨈繻䦔ھɰ汾㬻Ꮘḷ݋죩ᣍ

[5]Note that the <target-os>cygwin feature is different from the <flavor>cygwin subfeature of the gcc toolset, and you might need handle both explicitly if you also have a MinGW GCC installed.

עҢ㬌ؐԼtt class="docutils literal"><target-os>cygwin Ӑᰓڼtt class="docutils literal">gcc 餾߼﵄ד̘ <flavor>cygwin㬍 ȧ黃㍬ʱҲಗKMinGW GCC㬍 ģܐ蒪÷ȷ䦀핢}趌ؐԡ㍊

[6]

Windows users, your home directory can be found by typing:

ECHO %HOMEDRIVE%%HOMEPATH%

into a command prompt window.

WindowsӃ맿ɒԔڍ un̡ʾ綜/a>䰿ږ썊켈뒔ςunϔʾה캵Ė焿¼㺼/p>

ECHO %HOMEDRIVE%%HOMEPATH%
[7]On Unix and similar platforms, a debugging python and associated libraries are built by adding --with-pydebug when configuring the Python build. On Windows, the debugging version of Python is generated by the "Win32 Debug" target of the Visual Studio project in the PCBuild subdirectory of a full Python source code distribution.

ԚUNIXꍀˆƽ̨ɏ㬍 ԚŤփPython鹽花 ̭쓼tt class="docutils literal">--with-pydebug㬍 ɒԹ齨嬰԰搹thon찏๘㍊ԚWindowsɏ㬵犔঱Python㬍 ʇԚVisual StudioϮĿ֐㬍 Ӊᰗin32 Debug᱄ﱪ⺉꣬ 胏΄쾎듚 PythonԴ亂뷢Ŀ¼儐CBuildדĿ¼ᣍ