C++ Boost

Boost.Python

Building and Testing


Contents

Requirements
Building Boost.Python
Configuration
Results
Testing
Building your Extension Module
Build Variants

Requirements

Boost.Python version 2 は Python 2.2 または 最新版 が必要だ。 Python 1.5.2 以上で利用できる Boost.Python version 1 の非サポートアーカイブは ここ にある。

Building Boost.Python

すべての Boost.Python 拡張モジュールは boost_python 共有ライブラリとリンクしなければならない。 boost_python をビルドするには 通常 boost をインストールしたディレクトリ配下の libs/python/build から Boost.Build を使用しなさい。 (もしすでに boost をトップレベルからビルドしているなら、 仕事は終わっているので、これは効果がない)

Configuration

Python をインストールする時 Boost.Build を指すように、次の変数を設定することが必要。
Variable Name Semantics Default Notes
PYTHON_ROOT The root directory of your Python installation Windows: c:/tools/python Unix: /usr/local On Unix, this is the --with-prefix= directory used to configure Python
PYTHON_VERSION The The 2-part python Major.Minor version number 2.2 Be sure not to include a third number, e.g. not "2.2.1", even if that's the version you have.
PYTHON_INCLUDES path to Python #include directories Autoconfigured from PYTHON_ROOT
PYTHON_LIB_PATH path to Python library object. Autoconfigured from PYTHON_ROOT
PYTHON_STDLIB_PATH path to Python standard library modules Autoconfigured from PYTHON_ROOT
CYGWIN_ROOT path to the user's Cygwin installation Cygwin only. This and the following two settings are useful when building with multiple toolsets on Windows, since Cygwin requires a different build of Python.
GCC_PYTHON_ROOT path to the user's Cygwin Python installation $(CYGWIN_ROOT)/usr/local Cygwin only
GCC_DEBUG_PYTHON_ROOT path to the user's Cygwin pydebug build $(CYGWIN_ROOT)/usr/local/pydebug Cygwin only

Results

ビルドすると libs/python/build/bin-stage が boost root あるいは $(ALL_LOCATE_TARGET) で指定したサブディレクトリに作成される。 ライブラリは各々のツール毎にユニークなディレクトリに作成され、 変化するものはファイルシステムのどこかに作成される。 そして便利なものは bin-stage ディレクトリにコピーされる。 そのため複数のツールセットを一回でビルドすると、 後のツールセット・プロジェクトがそれより前の bin-stage にあるツールセットを上書きする。

Testing

Boost.Python をビルドしテストするには libs/python/test ディレクトリから始める。そして呼び出す。

bjam -sTOOLS=toolset test
これは Boost.Python version 1 のテストと例題をすべてアップデートする。 テストはデフォルトでは比較的情報が少ない。 より多くの情報を得るには、これを試しなさい。
bjam -sTOOLS=toolset -sPYTHON_TEST_ARGS=-v test
これは各々のテスト毎に Python のコードを出力し、テストが通った時に期待される出力がなされる。

Building your Extension Module

他のやり方もある。Boost.Python を使い拡張モジュールをビルドする最も簡単な方法は Boost.Build を使うことだ。 Boost.Build version 2 がリリースされるまで、プロジェクトをまたがった依存関係はサポートされない。 そのおかげで boost のインストールに新しいサブプロジェクトを追加することはとてもスムースにいく。 boost のインストールされたサブディレクトリ libs/python/example は最低限の例題を含んでいる(多くの特別なソースと供に)。 サブプロジェクトの例題をコピーするには、
  1. libs/python の中に libs/python/my_project というサブディレクトリを作る。
  2. libs/python/example/Jamfile をその新しいディレクトリにコピーする。
  3. Jamfile をプロジェクトに適切なものになるように編修する。"subproject" ルール呼び出しを最上位にするとか、ソースやターゲットの名称変更をしたいだろう。
もし boost のインストール設定を修正やコピーしたくないなら独自の Boost.Build プロジェクトを作成するしかない。 出発点として利用できる良く似た例は このアーカイブ が役立つ。 Jamfile と Jamrules ファイルを修正する必要があるだろう。 それらは Boost のインストール設定と新しいプロジェクトの相対位置に依存する。 拡張モジュールの自動テストはこの設定手段では利用できないことに注意しなさい。

Build Variants

Python 関連のターゲットは三つの 別形式(variant) を持ち、その設定はすべてサポートされる。そして ビルド 変数の設定により選択できる。

boost_python ライブラリの最初の二つの variant はデフォルトでビルドされる。 そしてデフォルトの Python 配布と互換である。 debug-python variant は Python デバッグバージョンの特別なビルドに一致する。 Unix プラットフォーム上では、Python をビルドする時に configure 設定で --with-pydebug を付け加えることにより、この python (デバッグ版)がビルドされる。 Windows 上では、Python 配布のサブディレクトリ PCBuild の中にある Visual C++ 6.0 プロジェクト PCBuild.dsw のターゲットを "Win32 Debug" にすることでデバッギング版 Python を作成できる。 拡張モジュールを Python デバッギング可能ビルドしたものは、非デバッグビルド Python にリンクレベルで 非互換 である。 Python を実際にデバッグビルドする人は少ないので(それは標準の配布には入っていない)。 標準 debug 形式は普通の Python に互換のモジュールをビルドする。

多くの Windws コンパイラで、拡張モジュールが -D_DEBUG 付きでビルドされた場合 Python はスペシャルデバッグ版 Python DLL としいてリンクすることはない。 Python をデフォルトで Windows にインストールした場合、このデバッグ DLLは供給されない。 そのため、BOOST_DEBUG_PYTHON を定義しないで Python.h をインクルードする場合 Boost.Python は一時的に _DEBUG を未定義にし、 boost/python/detail/wrap_python.hpp を使用する。

もしデバッグ版ライブラリを使用して、特別実行時チェックを利用したい場合、Python デバッグを再度可能にするため #define BOOST_DEBUG_PYTHON を定義し、boost_pythondebug-python 形式とリンクしなさい。

もし #define BOOST_DEBUG_PYTHON を定義しない場合、 拡張モジュールの中のどのソースファイルも通常の Python.h の代わりに #include <boost/python/detail/wrap_python.hpp> をインクルードしていることを確認しなさい。 さもないと、リンク非互換になるだろう。


© Copyright David Abrahams 2002. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided ``as is'' without express or implied warranty, and with no claim as to its suitability for any purpose.

Updated: O8 October, 2002 (David Abrahams)