![]() |
Building Hello World |
![]() |
![]() |
![]() |
まず最初にやりたい事は Hello World モジュールをビルドし、 自分で Python を使って試す事だろう。 この節では、それを達成するのに必要なステップの概略を示すつもりだ。 配布されるどの boost にも付属しているツール bjam を使用する。
![]() bjam なしでモジュールをビルドする方法はもちろん存在する。 ここに書かれていることは、"唯一無二の手段"と受け取るべきではない。 bjam とは別に、他のビルドツールも当然存在する。 |
詳細は省く。 目的は hello world モジュールを単純に作成し、Python のもとで実行することだ。 Boost.Python をビルドするための完全なリファレンスは building.html にある。 この bjam の後、二つの DLL が生成される。
Windows の場合
そして Unix の場合
このチュートリアルの例題はディレクトリ libs/python/example/tutorial の中で見付けることができる。 次のものがある。
hello.cpp ファイルは例題の hello world。 Jamfile は DLL を作成する最小限の bjam スクリプトだ。
まず bjam の実行形式を boost ディレクトリに置くか、 またはコマンドラインで bjam を実行できるようにしたパスに置くべきだ。 あらかじめビルドされた Boost.Jam 実行形式が、多くのプラットフォームで動作可能である。 たとえば Microsoft Windows bjam 実行形式は ここ(bjam.zip) からダウンロードできる。 ビルド済みの実行形式の完全なリストは Boost.Jam executable にある。
これが最小の Jamfile だ。
subproject libs/python/example/tutorial ;
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;
extension hello # Declare a Python extension called hello
: hello.cpp # source
<dll>../../build/boost_python # dependencies
;
最初に boost プロジェクト・ツリー中の位置を定義する必要がある。
たまたまチュートリアルの例は
/libs/python/example/tutorialの位置なので、こう定義される。
subproject libs/python/example/tutorial ;
次に、Python モジュールに必要とされる定義をインクルードする。
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
include python.jam ;
最後に、自分で作ったhello の拡張を定義する。
extension hello # Declare a Python extension called hello
: hello.cpp # source
<dll>../../build/boost_python # dependencies
;
bjamはオペレーティング・システムのコマンドライン・インタープリタ上で実行される。
インタプリタ起動。
環境設定が C++ コンパイラを呼び出せることを確認しなさい。 MSVC の場合には、Vcvars32.bat バッチファイルを起動させることを意味する。 例として。
C:\Program Files\Microsoft Visual Studio\VC98\bin\Vcvars32.bat
いくつかの環境変数は、Python モジュールを正しくビルドするために設定されなければならない。
set PYTHON_ROOT=c:/dev/tools/python
set PYTHON_VERSION=2.2
上記は Python が c:/dev/tools/python にインストールされていて、
そのバージョンは 2.2 であることが想像できるであろう。
このパスは適切に引っ張ってこれるに違いない。
バージョン番号の3番目は記述しないこと。
たとえば "2.2.1" は駄目、
そのようなバージョンを持っているにしても。
これで準備が揃った。チュートリアル"hello.cpp"と"Jamfile" が置いてあるlibs/python/example/tutorialへcdしなさい。
最後に
bjam -sTOOLS=msvc
これもまた Microsoft Visual C++ version 6 を使うことを想定している。 もし違うなら、他のツールの設定をしなければならない。 詳細は Building Boost Librariesを見なさい。
ビルドすると
cd C:\dev\boost\libs\python\example\tutorial
bjam -sTOOLS=msvc
...patience...
...found 1703 targets...
...updating 40 targets...
と、いろいろ表示されるが最後はこう表示される。
vc-C++ ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
runtime-link-dynamic\hello.obj
hello.cpp
vc-Link ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\msvc\debug\
runtime-link-dynamic\hello.pyd ..\..\..\..\libs\python\example\tutorial\bin\
hello.pyd\msvc\debug\runtime-link-dynamic\hello.lib
Creating library ..\..\..\..\libs\python\example\tutorial\bin\hello.pyd\
msvc\debug\runtime-link-dynamic\hello.lib and object ..\..\..\..\libs\python\
example\tutorial\bin\hello.pyd\msvc\debug\runtime-link-dynamic\hello.exp
...updated 40 targets...
すべてうまくいけば、次のものができる。
Windows 上なら
もし Unix 上なら
boost_python.dll は libs\python\build\binの中に、 hello.pyd は libs\python\example\tutorial\bin の中に見付かるはずだ。 ビルドが成功したなら、これらの DLL を Python インタプリタのもとでリンクできる。 Windows の場合、 これらのライブラリを Python 実行モジュールのあるディレクトリの中に置けばよい。
さあ Python を起動して、hello モジュールを実行しよう。
>>> import hello
>>> print hello.greet()
hello, world
There you go... Have fun!
![]() |
![]() |
![]() |
Copyright © 2002 David Abrahams
Copyright © 2002 Joel de Guzman
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.