PHP拡張開発(一)-最初のPHP拡張を構築する
5615 ワード
まず、システムにgccコンパイラ、適切なバージョンのbisonなどがインストールされていることを確認する必要があります.次に、ソースコードからPHPをコンパイルインストールするために実行する基本的なコマンドです.
基本的な拡張スケルトンの構築
PHP拡張開発では、
注:
ここでの
たとえば、ここでは $ cd .. $ vi ext/extdemo1/config.m4 $ ./buildconf $ ./configure --[with|enable]-extdemo1 $ make $ ./php -f ext/extdemo1/extdemo1.php $ vi ext/extdemo1/extdemo1.c $ make Repeat steps 3-6 until you are satisfied with ext/extdemo1/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.
extディレクトリの下に新しい拡張ディレクトリ
この場合、この拡張はコンパイルできません.
プロファイル
Autoconf構文はAUTOCNFドキュメントを参照
上記の extdemo 1これは最初のパラメータであり、 が確立された.の2番目のパラメータは、 が表示される.の3番目のパラメータは が表示されます.
2番目のマクロは
コンパイル拡張
このとき、PHPは
# cd php-src
# ./buildconf
# ./configure --enable-debug --enable-maintainer-zts --enable-cli
# make
# make install
基本的な拡張スケルトンの構築
PHP拡張開発では、
ext_skel
を使用して拡張構造スケルトンの作成を完了します.$ ./ext_skel
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
[--skel=dir] [--full-xml] [--no-help]
--extname=module module
--proto=file file
--stubs=file generate only function stubs in file
--xml generate xml documentation to be added to phpdoc-cvs
--skel=dir
--full-xml generate xml documentation for a self-contained extension (not yet implemented)
--no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled
注:
ext_skel
コマンドファイルは、ソースファイルのextディレクトリの下にあります.ここでの
--extname
パラメータは、作成する拡張子名であり、拡張子は小文字+下線で構成され、ext
ディレクトリで一意である必要があります.たとえば、ここでは
ext_demo_1
というPHP拡張子を作成します./vagrant/ext$ ./ext_skel --extname=ext_demo_1
Creating directory ext_demo_1
Creating basic files: config.m4 config.w32 .svnignore ext_demo_1.c php_ext_demo_1.h CREDITS EXPERIMENTAL tests/001.phpt ext_demo_1.php [done].
To use your new extension, you will have to execute the following steps:
extディレクトリの下に新しい拡張ディレクトリ
ext_demo_1
が表示されます./vagrant/ext/ext_demo_1$ ls
config.m4 CREDITS ext_demo_1.c php_ext_demo_1.h
config.w32 EXPERIMENTAL ext_demo_1.php tests
この場合、この拡張はコンパイルできません.
config.m4
ファイルを編集する必要があります.プロファイル
config.m4
コンフィギュレーション・ファイルconfig.m4
は、UNIXビルド・システム拡張がサポートするconfigure
オプションと、拡張に必要な追加のライブラリ、どのソース・ファイルを含むかなどを示します.このファイルは、GNUのautoconf
構文を使用し、dnl
で始まる動作注釈で、カッコ([および])に含まれる文字列を使用します.Autoconf構文はAUTOCNFドキュメントを参照
PHP_ARG_ENABLE(ext_demo_1, whether to enable ext_demo_1 support,
[ --enable-ext_demo_1 Enable ext_demo_1 support])
if test "$PHP_EXT_DEMO_1" != "no"; then
PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)
PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)
fi
上記の
autoconf
のプロファイルは、1番目のマクロPHP_ARG_ENABLE
であり、3つのパラメータを含む../configure
にenable-ext_demo_1
というオプション./configure
コマンドが拡張プロファイルに処理すると、そのパラメータの内容./configure
コマンドのヘルプで、./configure --help
を使用すると2番目のマクロは
PHP_NEW_EXTENSION
で、拡張モジュールと拡張の一部としてコンパイルする必要があるソースファイルを宣言します.複数のソースファイルが必要な場合は、スペース区切り、3番目のパラメータ$ext_を使用します.sharedは呼び出しPHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)
に関係する.PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)
コンパイル拡張
config.m4
ファイルを修正した後、PHPと拡張子をコンパイルします./vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php
/vagrant$ make
/vagrant$ sudo make install
Installing PHP SAPI module: cgi
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/man/man1/
page: phpize.1
page: php-config.1
/vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
このとき、PHPは
/usr/local/php
ディレクトリの下にインストールされ、このディレクトリに入ると、以下のファイルが表示されます./usr/local/php$ ls
bin include lib man
/usr/local/php/bin
ディレクトリに入り、次のコマンドを実行します./usr/local/php/bin$ ./php --info|grep demo
Configure Command => './configure' '--disable-libxml' '--enable-ext_demo_1' '--disable-dom' '--disable-simplexml' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--prefix=/usr/local/php'
ext_demo_1
ext_demo_1 support => enabled
phpinfo()
の拡張サポートが有効になっていることがわかります.上記の手順でインストールされた拡張には、confirm_ext_demo_1_compiled(arg)
という名前の拡張機能が含まれています.実行結果は次のとおりです./usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');"
Congratulations! You have successfully modified ext/ext_demo_1/config.m4. Module mylxsw is now compiled into PHP.
ext_demo_1
の拡張インストールが成功したことがわかります.拡張開発の詳細については、AICODEに移動してください.CC.