ExtStudy | |
Windows系はあまりよく知らないので、UNIX系での開発を行っていきます。Windowsを利用されている方は、VMwareなどで開発環境を構築してください。
id:wadap さんのこちらの記事がすごく分かりやすいです。
以下、Ubuntu7.10、PHP5.2.3の環境で確認しています。
上のwadapさんの記事に説明されているパッケージに加えて、
php5-cli、php5-dev、php-pear をインストールしてください。これらに必要な関連パッケージも一緒にインストールされます。
# sudo apt-get install php5-cli php5-dev php-pear
うまく入ったか確認
$ php -v PHP 5.2.3-1ubuntu6.3 (cli) (built: Jan 10 2008 09:38:37) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
$ sudo pear install CodeGen_PECL downloading CodeGen_PECL-1.1.0.tgz ... Starting to download CodeGen_PECL-1.1.0.tgz (91,997 bytes) .....................done: 91,997 bytes downloading CodeGen-1.0.4.tgz ... Starting to download CodeGen-1.0.4.tgz (35,065 bytes) ...done: 35,065 bytes install ok: channel://pear.php.net/CodeGen-1.0.4 install ok: channel://pear.php.net/CodeGen_PECL-1.1.0
で、動かしてみる
$ pecl-gen
Usage:
pecl-gen [-h] [--force] [--experimental] [--version]
[--extname=name] [--proto=file] [--skel=dir] [--stubs=file]
[--no-help] [--xml[=file]] [--full-xml] [--function=proto] [specfile.xml]
-h|--help this message
-f|--force overwrite existing directories
-d|--dir output directory (defaults to extension name)
-l|--lint check syntax only, don't create output
--linespecs generate #line specs
-x|--experimental deprecated
--function create a function skeleton from a proto right away
--version show version info
the following options are inherited from ext_skel:
--extname=module module is the name of your extension
--proto=file file contains prototypes of functions to create
--xml generate xml documentation to be added to phpdoc-cvs
these wait for functionality to be implemented and are ignored for now ...
--stubs=file generate only function stubs in file
--no-help don't try to be nice and create comments in the code
and helper functions to test if the module compiled
these are accepted for backwards compatibility reasons but not used ...
--full-xml generate xml documentation for a self-contained extension
(this was also a no-op in ext_skel)
--skel=dir path to the skeleton directory
(skeleton stuff is now self-contained)
と出てきて、pecl-genが動いてることが確認できました。
ビルド環境が整っているか確認するために、空のExtensionを作ってみましょう。空のExtensionとは、関数すらない何もしないExtensionで、あるのはモジュールの名前だけです。
先ほどインストールしたCodeGen_PECLを利用します。
$ pecl-gen --extname=hoge Your extension has been created in directory ./hoge. See ./hoge/README and/or ./hoge/INSTALL for further instructions.
これは、モジュール名を "hoge" として、空のスケルトンを作っています。実行すると、モジュール名の名前でディレクトリが作られています。
$ ls hoge EXPERIMENTAL README config.m4 config.w32 hoge.c hoge.dsp package.xml package2.xml php_hoge.h
先ほど出来たhogeディレクトリにおりて、phpize、./configure、makeの順に呼びます。
$ cd hoge/
$ phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
$ ./configure
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
...(snip)
configure: creating ./config.status
config.status: creating config.h
$ make
/bin/bash /home/iogiwara/work/extstudy/080122/1/hoge/libtool --mode=compile gcc ...
...
----------------------------------------------------------------------
Libraries have been installed in:
/home/iogiwara/work/extstudy/080122/1/hoge/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
ここまで出たら、うまくコンパイルできています。
空なのでテストがないので、make testは省略。
make installします。
$ sudo make install Installing shared extensions: /usr/lib/php5/20060613+lfs/
そうすると、この環境の場合、 /usr/lib/php5/20060613+lfs/ というディレクトリ下に hoge.so がコピーされます。(このパスは、環境によって異なります)
これだけだと、PHPには組み込まれません。読み込むように設定を書く必要があります。
extenison=hoge.so
というやつで、ちょっとテストしたいときには、コマンドラインでの -d オプションで設定して呼び出します。
コマンドラインの -m オプションでは、インストールされているモジュール一覧が表示されるので、
$ php -dextension=hoge.so -m [PHP Modules] bcmath bz2 calendar ... hoge ... zip zlib [Zend Modules]
と叩いて、モジュール一覧にhogeが含まれていればOK。
きちんと組み込む場合には、iniファイルを追加することになります。iniファイルの場所は $php --iniで調べることができます。
$ php --ini Configuration File (php.ini) Path: /etc/php5/cli Loaded Configuration File: /etc/php5/cli/php.ini Scan for additional .ini files in: /etc/php5/cli/conf.d Additional .ini files parsed: /etc/php5/cli/conf.d/curl.ini, /etc/php5/cli/conf.d/pdo.ini
この場合、最初に読み込む設定ファイルが /etc/php5/cli/php.ini で、そのあと、/etc/php5/cli/conf.d ディレクトリ以下にあるiniファイルを読みに行って、curl.ini と pdo.ini が読まれているということが分かります。
/etc/php5/cli/conf.d 以下に設定ファイルを加えることにします。 /etc/php5/cli/conf.d 下に hoge.ini という名前のファイルを作り
extension=hoge.so