boost::Pythonを使ってみよう
まずは、boostを入れまっしょい。
<余談>
時折考えるのだが、国内のC++使っている率って、どのくらいなんだろう?
自分の周りは、拡張子を.CPPにし、実際には C で使っている例が多くて、 C++ を使っていると、奇異の目で見られるんだよね。
better Cとしてすら使ってないっちゅうか…、boost になると更に…。みゅぅ。
Boost C++ Libraries
http://www.boost.org/
より、Downloadのページより、以下のファイルをゲットするのよね。(実際には、exeファイルもあるんだが、まぁ、ビルドしてやるのが面白いでしょ?)
- boost_1_33_0.tar.gz
- boost-jam-3.1.10-1-ntx86.zip
ビルドに関しては、以下を一読しておく事を勧める。
Getting Started
http://www.boost.org/more/getting_started.html#Preparation
つぅか、先達者の方のページの内容。時期が違うせいか、少しずつ異なっている雰囲気。うぅぅ。
で、ファイルを展開。
C:\wacky\ComLib>tar xzvf boost_1_33_0.tar.gz ... boost_1_33_0/tools/wave/build/Jamfile boost_1_33_0/tools/wave/build/Jamfile.v2 boost_1_33_0/tools/wave/cpp.cpp boost_1_33_0/tools/wave/cpp.hpp boost_1_33_0/tools/wave/cpp_config.hpp boost_1_33_0/tools/wave/cpp_version.hpp boost_1_33_0/tools/wave/stop_watch.hpp boost_1_33_0/tools/wave/trace_macro_expansion.hpp boost_1_33_0/wiki/ boost_1_33_0/wiki/index.html
ここで、boost_1_33_0 フォルダに、boost-jam-3.1.10-1-ntx86.zipを展開して出てきた bjam.exe を置く。
ここから、ビルドする。
(ビルドする際、PYTHON_ROOTとPYTHON_VERSIONを設定する必要があるよ)
C:\wacky\ComLib\boost_1_33_0>"C:\Program Files\Microsoft Visual Studio .NET 2003 \Common7\Tools\vsvars32.bat" Setting environment for using Microsoft Visual Studio .NET 2003 tools. (If you have another version of Visual Studio or Visual C++ installed and wish to use its tools from the command line, run vcvars32.bat for that version.) C:\wacky\ComLib\boost_1_33_0>set PYTHON_ROOT=C:\tool2\Python23 C:\wacky\ComLib\boost_1_33_0>set PYTHON_VERSION=2.3 C:\wacky\ComLib\boost_1_33_0>ls Jamfile boost index.htm rst.css Jamfile.v2 boost-build.jam libs status Jamrules boost.css more tools LICENSE_1_0.txt boost.png people wiki README doc project-root.jam bjam.exe google_logo_40wht.png regression C:\wacky\ComLib\boost_1_33_0>bjam -sTOOLS=vc7.1 install vc7.1-tools.jam: No such file or directory ****************************************************** Building Boost.Iostreams with bzip2 support disabled. ECHO To enable bzip2, cons ult the Boost.Iostreams documentation ****************************************************** ****************************************************** Building Boost.Iostreams with zlib and gzip support disabled. To enable zlib and gzip, consult the Boost.Iostreams documentation ****************************************************** Building Boost.Regex with the optional Unicode/ICU support disabled. Please refer to the Boost.Regex documentation for more information (and if you don't know what ICU is then you probably don't need it). vc7.1-tools.jam: No such file or directory C:\wacky\ComLib\boost_1_33_0\tools/build/v1\allyourbase.jam:641: in C++ rule C++-action unknown in module C:\wacky\ComLib\boost_1_33_0\tools/build/v1\allyourbase.jam:1138: in Object C:\wacky\ComLib\boost_1_33_0\tools/build/v1\allyourbase.jam:1203: in Objects C:\wacky\ComLib\boost_1_33_0\tools/build/v1\boost-base.jam:150: in dll-files C:\wacky\ComLib\boost_1_33_0\tools/build/v1\boost-base.jam:1968: in subvariant-t arget C:\wacky\ComLib\boost_1_33_0\tools/build/v1\boost-base.jam:2405: in stage Jamfile:195: in load-jamfiles C:\wacky\ComLib\boost_1_33_0\tools\build\v1\bootstrap.jam:21: in boost-build C:\wacky\ComLib\boost_1_33_0\boost-build.jam:16: in module scope C:\wacky\ComLib\boost_1_33_0>bjam "-sTOOLS=vc-7_1" install ... MkDir1 C:\Boost\include\boost-1_33\boost\wave\util FileClone C:\Boost\include\boost-1_33\boost\wave\util\cpp_ifblock.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\cpp_include_paths.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\cpp_iterator.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\cpp_macromap.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\cpp_macromap_predef.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\cpp_macromap_utils.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\eat_whitespace.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\file_position.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\flex_string.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\functor_input.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\insert_whitespace_detectio n.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\interpret_pragma.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\iteration_context.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\macro_definition.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\macro_helpers.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\pattern_parser.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\symbol_table.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\time_conversion_helper.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\transform_iterator.hpp FileClone C:\Boost\include\boost-1_33\boost\wave\util\unput_queue_iterator.hpp ...updated 5283 targets... C:\wacky\ComLib\boost_1_33_0>bjam "-sTOOLS=vc-7_1" stage ... HardLink stage\lib\libboost_test_exec_monitor-vc71-mt-s.lib ...on 200th target... HardLink stage\lib\libboost_test_exec_monitor-vc71-mt.lib HardLink stage\lib\libboost_unit_test_framework-vc71-sgd.lib HardLink stage\lib\libboost_unit_test_framework-vc71-mt-sgd.lib HardLink stage\lib\libboost_unit_test_framework-vc71-mt-gd.lib HardLink stage\lib\libboost_unit_test_framework-vc71-s.lib HardLink stage\lib\libboost_unit_test_framework-vc71-mt-s.lib HardLink stage\lib\libboost_unit_test_framework-vc71-mt.lib HardLink stage\lib\boost_thread-vc71-mt-gd.lib HardLink stage\lib\boost_thread-vc71-mt.lib HardLink stage\lib\libboost_thread-vc71-mt-sgd.lib HardLink stage\lib\libboost_thread-vc71-mt-gd.lib HardLink stage\lib\libboost_thread-vc71-mt-s.lib HardLink stage\lib\libboost_thread-vc71-mt.lib HardLink stage\lib\libboost_wave-vc71-sgd.lib HardLink stage\lib\libboost_wave-vc71-mt-sgd.lib HardLink stage\lib\libboost_wave-vc71-mt-gd.lib HardLink stage\lib\libboost_wave-vc71-s.lib HardLink stage\lib\libboost_wave-vc71-mt-s.lib HardLink stage\lib\libboost_wave-vc71-mt.lib ...updated 218 targets...
こうすると、stageフォルダに、dllとlibファイルが出来上がった。
$ dir stage\lib ... 2005/08/13 14:58 7,270,454 libboost_wserialization-vc71-s-1_33.lib 2005/08/13 14:58 7,270,454 libboost_wserialization-vc71-s.lib 2005/08/13 14:55 17,257,356 libboost_wserialization-vc71-sgd-1_33.lib 2005/08/13 14:55 17,257,356 libboost_wserialization-vc71-sgd.lib 216 個のファイル 1,303,032,528 バイト 2 個のディレクトリ 97,061,240,832 バイトの空き領域
でも、VC7のフォルダに突っ込んで欲しいしぃと思い、--prefix を付けてみる事にした。
C:\wacky\ComLib\boost_1_33_0>bjam "-sTOOLS=vc-7_1" "--prefix=C:\Program Files\Mi crosoft Visual Studio .NET 2003\Vc7" install ****************************************************** Building Boost.Iostreams with bzip2 support disabled. ECHO To enable bzip2, cons ult the Boost.Iostreams documentation ****************************************************** ****************************************************** Building Boost.Iostreams with zlib and gzip support disabled. To enable zlib and gzip, consult the Boost.Iostreams documentation ****************************************************** Building Boost.Regex with the optional Unicode/ICU support disabled. Please refer to the Boost.Regex documentation for more information (and if you don't know what ICU is then you probably don't need it). ... C:\wacky\ComLib\boost_1_33_0>bjam "-sTOOLS=vc-7_1" "--prefix=C:\Program Files\Mi crosoft Visual Studio .NET 2003\Vc7" install ... FileClone C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\boost-1 _33\boost\wave\util\pattern_parser.hpp FileClone C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\boost-1 _33\boost\wave\util\symbol_table.hpp FileClone C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\boost-1 _33\boost\wave\util\time_conversion_helper.hpp FileClone C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\boost-1 _33\boost\wave\util\transform_iterator.hpp FileClone C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\boost-1 _33\boost\wave\util\unput_queue_iterator.hpp ...updated 3787 targets...
こうすると、VC7.1のフォルダ下に、ファイルが出来上がる模様。
インクルードファイル | vc7\include\boost-1_33 |
ライブラリファイル | vc7\lib |
後で気付いたんだが、--prefixを付けないと、ドライブのルートに boost フォルダを作っちゃう模様。うひゃ。
インクルードファイル | C:\Boost\include\boost-1_33 |
ライブラリファイル | C:\Boost\lib |
で、多分、install を付けて bjam を使うと、Boostフォルダを作るが、stage を付けると、boostを展開したフォルダをルートにして stage フォルダを作って、そこにファイルをコピーするようだ。
インクルードファイル | boost_1_33_0\boost |
ライブラリファイル | boost_1_33_0\stage\lib |
で、ここまでで boost ライブラリが出来たので、boost::Python を使って 拡張モジュールを作ってみよう。
まず、VC7.1を起動。
1.ファイル→新規作成→プロジェクト を選択。
2.新しいプロジェクト欄で、"Visual C++ プロジェクト"の"Win32 コンソール プロジェクト"を選択し、プロジェクトを作る。
3."Win32 アプリケーション ウィザード"が起動する。
ここで、"アプリケーションの設定"を選択し、以下を選択。
- アプリケーションの種類:DLL
- 追加のオプション:空のプロジェクト
3.んで、ソリューション ペイン に対して、以下のソースファイルを追加する。("既存項目の追加"で追加できる)
#include <boost/python.hpp> using namespace boost::python; #pragma comment(lib, "boost_python-vc71-mt-gd.lib") char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(test) { def("greet", greet); }
4.ツール→オプションを選択し、プロジェクト欄に、インクルードとライブラリパスに先に作ったboostライブラリへのパスを追加する。その時、Pythonのパスも追加する。
- インクルードパス:C:\Boost\include\boost-1_33
- インクルードパス:C:\tool2\Python23\include
- ライブラリパス:C:\Boost\lib
- ライブラリパス:C:\tool2\Python23\libs
すると、Debugフォルダに拡張モジュールである test.dll が出来る。
6.早速 python を起動し、import してみよう。
--------------------------- python.exe - コンポーネントが見つかりません --------------------------- boost_python-vc71-mt-gd-1_33.dll が見つからなかったため、このアプリケーションを開始できませんでした。アプリケーションをインストールし直すとこの問題は解決される場合があります。 --------------------------- OK ---------------------------
DLLファイルをロードできずに import に失敗。
7.boost/lib フォルダに、 boost_python-vc71-mt-gd-1_33.dll ファイルがあるので、Windowsの検索パス内に(test.dll があるフォルダとか)配置する。
8.で、再び import する。
>>> import test >>> print dir(test) ['__doc__', '__file__', '__name__', 'greet'] >>> print test.greet() hello, world >>>
動いた、動いた。