在linux开发PHP扩展相对windows配置容易的的多,但是有时候我们是用windows来做服务器的,就不得不编译用于windows版本的dll扩展。这里讲一下在vs2008下开发PHP扩展的环境搭建。大体思路如下(有的配置需要根据你的情况而定,所以下面的的步骤并不一定在你的PC上可以搭建成功。但是大体步骤是这样,这里只是抛砖引玉):
第一:准备工具
1.vs2008
2.php
http://windows.php.net/download/
php-5.3.8-src
http://windows.php.net/downloads/releases/php-5.3.8-src.zip
VC9 x86 Non Thread Safe
http://windows.php.net/downloads/releases/php-5.3.8-nts-Win32-VC9-x86.zip
VC9 x86 Thread Safe
http://windows.php.net/downloads/releases/php-5.3.8-Win32-VC9-x86.zip
使用ISAPI的方式来运行PHP就用Thread Safe(线程安全)的版本
使用FastCGI模式运行PHP的话就没有必要用线程安全检查了,用None Thread Safe(NTS,非线程安全)的版本能够更好的提高效率。
3.必要库
http://www.php.net/extra/bindlib_w32.zip
http://www.php.net/extra/win32build.zip
第二:步步为营搭建开发环境
我这里没有用到cygwin,因为我的开发只是一个很简单的demo,没有使用第三方了类库。如果是把linux下拿过来的扩展项目,可能用到一些库。可能用cygwin会比较好。但是没有cygwin完全可以在window下开发。具体如下:
1.目录结构:
F:\php_win
F:\php_win>dir
F:\php_win 的目录
2011-11-23 21:56 <DIR> php-5.3.8
2011-11-23 20:43 <DIR> php-5.3.8-nts-Win32-VC9-x86
2011-11-18 14:19 15,659,969 php-5.3.8-nts-Win32-VC9-x86.zip
2011-11-23 15:07 20,745,577 php-5.3.8-src.zip
2011-11-23 21:51 <DIR> win32build
我把第三方包都放到合并到一起放在win32build目录下。你可以根据情况而定
2.安装vs2008这里就不多说了
我的安装在C:\vc文件夹下面。
3.配置PATH
安装完成vs2008,有的编译命令可能不能直接运行。加入PATH,可以避免这个问题。
这里有两个目录要注意
一个是C:\vc\vc2008compiler\bin
下面有cl.exe\link.exe等等
第二个是C:\vc\Microsoft Visual\VC2008 Professional Edition\SDK\V6.0A\bin
把他们加入到PATH环境变量中。
4.建立配置PHP开发环境。
(1)。lamp开发环境,因为我们可能会用到php.exe来执行一些脚本。
(2)。PHP二次开发环境。PHP开发环境的配置很繁琐,但是官方提供了shell脚本,让我们可以来快速的构建一个可以用的PHP二次开发环境。
进入F:\php_win\php-5.3.8>。执行cl,确保能够执行。具体如下:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cl
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 15.00.21022.08 版
版权所有(C) Microsoft Corporation。保留所有权利。
用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]
C:\Documents and Settings\Administrator>f:
F:\>cd php_win
F:\php_win>cd php-5.3.8
F:\php_win\php-5.3.8>buildconf
Rebuilding configure.js
Now run 'configure --help'
F:\php_win\php-5.3.8>cscript /nologo configure.js --with-php-build="F:\php_win\win32build" --without-libxml --disable-o
dbc
Saving configure options to config.nice.bat
Checking for cl.exe ... <in default path>
Detected compiler MSVC9 (Visual C++ 2008)
Detected 32-bit compiler
Checking for link.exe ... D:\cygwin\bin
Checking for nmake.exe ... <in default path>
Checking for lib.exe ... <in default path>
Checking for bison.exe ... F:\php_win\win32build\bin
Checking for re2c.exe ... <not found>
Parsers will not be regenerated
Checking for zip.exe ... <not found>
Checking for lemon.exe ... <not found>
Checking for mc.exe ... C:\vc\Microsoft Visual\VC2008 Professional Edition\SDK\V6.0A\bin
Checking for mt.exe ... C:\vc\vc2008compiler\bin
Build dir: Release_TS
PHP Core: php5ts.dll and php5ts.lib
Checking for wspiapi.h ... <not found>
Checking for wspiapi.h ... <not found>
Enabling SAPI sapi\cgi
Enabling SAPI sapi\cli
Enabling extension ext\bcmath
Enabling extension ext\calendar
Checking for library oleaut32.lib ... <not found>
Enabling extension ext\com_dotnet
Checking for mscoree.h ... <not found>
Checking for mscoree.h ... <not found>
Enabling extension ext\ctype
Enabling extension ext\date
Enabling extension ext\ereg
Enabling extension ext\filter
Enabling extension ext\ftp
Checking for library libjpeg_a.lib;libjpeg.lib ... <not found>
WARNING: gd not enabled; libraries and headers not found
Enabling extension ext\hash
Checking for library libiconv_a.lib ... <not found>
Checking for library libiconv.lib ... <not found>
Checking for library iconv_a.lib ... <not found>
Checking for library iconv.lib ... <not found>
WARNING: iconv support can't be enabled, libraries or headers are missing
Enabling extension ext\json
Checking for library ws2_32.lib ... <not found>
Enabling extension ext\pcre
Enabling extension ext\reflection
Enabling extension ext\session
Enabling extension ext\spl
Checking for timelib_config.h ... ext/date/lib
Enabling extension ext\standard
Enabling extension ext\tokenizer
Checking for zlib.h ... F:\php_win\win32build\include
Enabling extension ext\zip
Checking for library zlib_a.lib;zlib.lib ... ..\win32build\lib\zlib.lib
Checking for zlib.h ... F:\php_win\win32build\include
Enabling extension ext\zlib
WARNING: dom support can't be enabled, libxml is not enabled
Enabling extension ext\phar
Native OpenSSL support in Phar disabled
WARNING: simplexml not enabled; libraries and headers not found
WARNING: xml support can't be enabled, libraries or headers are missing
Creating build dirs...
Generating files...
Generating Makefile
Generating main/internal_functions.c
Generating main/config.w32.h
Generating phpize
Done.
Enabled extensions:
-----------------------
| Extension | Mode |
-----------------------
| bcmath | static |
| calendar | static |
| com_dotnet | static |
| ctype | static |
| date | static |
| ereg | static |
| filter | static |
| ftp | static |
| hash | static |
| json | static |
| pcre | static |
| phar | static |
| reflection | static |
| session | static |
| spl | static |
| standard | static |
| tokenizer | static |
| zip | static |
| zlib | static |
-----------------------
Enabled SAPI:
-------------
| Sapi Name |
-------------
| cgi |
| cli |
-------------
-------------------------------------------
| | |
-------------------------------------------
| Build type | Release |
| Thread Safety | Yes |
| Compiler | MSVC9 (Visual C++ 2008) |
| Architecture | x86 |
-------------------------------------------
Type 'nmake' to build PHP
F:\php_win\php-5.3.8>
如果出现上类似的提示, 说明你的PHP开发环境已经搭建成功。如果执行脚步提示错误,你可以搜索错误消息,具体解决。从上面的不难看出一些很重要的信息。
其实最主要的就是生成config.w32.h。即PHP开发环境的配置文件。如果没有这个脚本,windows下开发php,简直太悲惨了。
5.开发PHP扩展的方法。
到这里我们就可以建立开发PHP扩展了,但是无从下手,我们该怎么办,其实PHP也给我们提供了很好用的工具来建立一个PHP扩展的骨架。就是用F:\php_win\php-5.3.8\ext下的ext_skel_win32.php
如何运行,以php结尾,说明依赖php。下面看具体方法,我建立一个名为’test‘的扩展。
这里要确保你的php.exe可以直接在cmd下使用,具体还是加入Path路径。
虽然有ext_skel_win32.php,用php执行,但是我们不知道具体的内容。
其实执行如下命令:
F:\php_win\php-5.3.8\ext>php ext_skel_win32.php
ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
[--skel=dir] [--full-xml] [--no-help]
--extname=module module is the name of your extension
--proto=file file contains prototypes of functions to create
--stubs=file generate only function stubs in file
--xml generate xml documentation to be added to phpdoc-cvs
--skel=dir path to the skeleton directory
--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
Warning: fopen(/.php): failed to open stream: No such file or directory in F:\php_win\php-5.3.8\ext\ext_skel_win32.php o
n line 52
F:\php_win\php-5.3.8\ext>
可以看到如何使用这php脚本。大体命令如下
F:\php_win\php-5.3.8\ext>php ext_skel_win32.php --extname=test
Creating directory test
Creating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [do
ne].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/test/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-test
5. $ make
6. $ ./php -f ext/test/test.php
7. $ vi ext/test/test.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/test/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.
F:\php_win\php-5.3.8\ext>
这样就成功创建了一个php扩展骨架,你可以在里面修改。具体的还要好好研究PHP API。具体扩展的位置就在ext目录下,打开可以看到test文件夹,这就是刚才命令创建的。
F:\php_win\php-5.3.8\ext\test 的目录
2011-11-29 16:57 <DIR> .
2011-11-29 16:57 <DIR> ..
2011-11-29 16:57 16 .svnignore
2011-11-29 16:57 1,970 config.m4
2011-11-29 16:57 282 config.w32
2011-11-29 16:57 4 CREDITS
2011-11-29 16:57 0 EXPERIMENTAL
2011-11-29 16:57 2,768 php_test.h
2011-11-29 16:57 5,128 test.c
2011-11-29 16:57 4,954 test.dsp
2011-11-29 16:57 500 test.php
2011-11-29 16:57 <DIR> tests
9 个文件 15,622 字节
3 个目录 7,441,883,136 可用字节
F:\php_win\php-5.3.8\ext\test>
这样一个php扩展的框架已经创建完成了。下面就是配置使用vs2008开发这个扩展。后续文章讲解。