![]() |
QuickStart |
![]() |
![]() |
![]() |
Boost Python ライブラリは Python と C++ との仲介役を果たすフレームワークである。 速攻でシームレスに C++ クラスや関数そしてオブジェクトを Python に公開でき、 逆になにも特別なツールはいらない — C++ コンパイラだけは必要。 C++ インタフェースをラップするのに余計なことをしないよう設計されたので、 ラップするために C++ コードを変更する必要はまったくない。 サードパーティ製ライブラリを Python に公開するのに、 Boost.Python を理想的な作りにした。 ライブラリの進歩的メタプログラミング技術は、ユーザにとって構文規則を簡単にし、 そのラッピングコードは叙述的なインターフェース定義言語 (IDL) のような外見を持つ。
下記 C/C++ の古典、"hello, world" ではじめよう。
C++ 関数:
char const* greet()
{
return "hello, world";
}
これは Boost.Python ラッパを書くことにより Python に公開できる。
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("greet", greet);
}
これでおしまい。これは共有ライブラリとしてビルドできる。 生成されたダイナミックライブラリは Python で認識可能である。 下記は Python セッションの一例。
>>> import hello
>>> print hello.greet()
hello, world
Next stop... Building your Hello World module from start to finish...
![]() |
![]() |
![]() |
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.