在Windows上开发PHP扩展模块

在Windows上开发PHP扩展模块

一、开发环境搭建

1.      Windows

Win7 64-bit旗舰版(Ultimate)

2.      LAMP环境

我使用的是WAMP 2.2D (32bit)。Windows上还有另外一款常用的LAMP环境XAMPP。不过因为XAMPP包含了我不需要的Perl,所以我选用了WAMP。

另外一方面,无论选用哪种LAMP环境,关键的是它打包的Apache和PHP版本(原因我们马上就会看到)。WAMP 2.2D (32-bit)打包的是Apache 2.2.21和PHP 5.3.10。

3.      PHP源码

WAMP中只包含编译好的PHP的EXE/DLL文件。而开发PHP扩展模块必须依赖PHP的源码。注意必须下载和LAMP环境中PHP版本完全一致的源码。www.php.net上给出的直接链接是最新版本(我写这篇文章的时候是5.4.3)。为了下载5.3.10版本,我们必须到PHP站点的release page (http://www.php.net/releases)上去找。

使用ISAPI的方式来运行PHP就用Thread Safe(线程安全)的版本;使用FastCGI模式运行PHP的话就没有必要用线程安全检查了,用None Thread Safe (NTS,非线程安全)的版本能够更好的提高效率。注意:WAMP 2.2D (32-bit)是TS版本的PHP。

4.      第三方库

像编译其它从Linux移植到Windows上的库一样,我们需要一些第三方库的源码。

-         bindlib (http://www.php.net/extra/bindlib_w32.zip)

处理绑定变量的数据结构(类似lambda表达式或者量化公式)的库

-         win32build (http://www.php.net/extra/win32build.zip)

在Windows上编译PHP的工具

         将这两个ZIP文件解压在同一个目录下。加上PHP源码,我的目录结构如下图:

         E:\php_win\php-5.3.10           以下记为”[PHP Source]”

                             \win32build\bin        在win32build.zip中

                             \win32build\include     在win32build.zip中

                             \win32build\lib         在win32build.zip中

                             \win32build\bindlib_w32 在bindlib_w32.zip中

5.      Cygwin (http://www.cygwin.com)

Cygwin是在调用PHP扩展模块生成工具ext_skel_win32.php时使用。注意:安装时提示选取镜像站点下载模块时,选择.cn结尾的站点速度最快。

安装完成Cygwin之后(安装路径记为[Cygwin Path]),需要将[PHP Source]\ext\ext_skel_win32.php文件中的$cygwin_path设置为[Cygwin]\bin目录路径。

6.      Visual Studio 2008

有些文章上说要使用VS6.0,其实是不准确的。应该是使用和LAMP环境中PHP的编译源相同的VS版本。我使用的PHP 5.3.10是使用VC++ 9.0编译的,所以我必须使用VS2008来编译。

7.      将如下路径添加入Windows系统的PATH环境变量

-         php.exe

-         E:\php_win\win32build\bin

为了调用bison.exe(被[PHP Source]\configure.bat调用)

-         C:\Program Files(x86)\Microsoft Visual Studio 9.0\Common7\Tools

为了调用vsvars32.bat(设置VS2008命令行环境的批处理)

二、创建步骤

1.      打开Windows Console并定位到[PHP Source]

2.      执行vsvars32.bat,进入VS命令行环境

3.      执行buildconf.bat,设置configure.js所需环境

成功的话显示如下信息:

Rebuilding configure.js

Now run 'configure --help'

4.      执行configure.bat,创建PHP编译环境

成功的话显示如下信息:

E:\php_win\php-5.3.10>buildconf
Rebuilding configure.js
Now run 'configure --help'

E:\php_win\php-5.3.10>configure
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

E:\php_win\php-5.3.10>

以上啰啰嗦嗦很多信息,其实最主要的就是生成config.w32.h。

5.      进入[PHP Source]\ext目录

6.      执行php ext_skel_win32.php –extname=myhello

ext_skel_win32.php是PHP源码提供的PHP扩展模块代码架构生成工具。我们在前面安装Cygwin时已经提到过它。因为它调用了很多Linux命令,所以必须安装Cygwin并设置它的$cygwin_path变量。

“--extname=myhello"很好理解,用于指定扩展模块名称。执行成功之后,会在[PHP Source]\ext目录下看到新目录”myhello”。

友情提示:到这里我们不再需要命令行操作了。所以如果没有DOS情节,你可以关掉那个小黑窗口了。

7.      进入myhello目录,双击myhello.dsp

VS2008会提示你升级VS项目,闭着眼睛一路Next直到项目在VS2008中被打开。你会发现现在只有两个有效文件:myhello.c, php_myhello.h。

8.      打开myhello项目的属性对话框,定位到Linker\General节点

为Additional Library Directories属性添加php5ts.lib所在的目录。注意这个lib是在LAMP环境的PHP目录中。我的php5ts.lib在E:\WAMP\bin\php\php5.3.10\dev目录中。

当然如果你的LAMP环境不是使用TS版PHP,那么应该是php5.lib。可以再回到myhello项目的属性对话框,定位到Linker\General节点,查看Additional Dependenices属性来确认你使用的PHP库版本。

9.      将Solution Configurations设置为Release_TS,开始编译

由于Debug_TS的项目配置为链接php5ts_debug.lib,所以直接使用Debug_TS编译会提示找不到php5ts_debug.lib。

一切顺利的话,就可以在[PHP Source]\ext\Release_TS目录下发现编译好的php_myhello.dll了。

三、部署步骤

1.      将php_myhello.dll复制到LAMP的PHP扩展模块目录中

我的环境里是E:\WAMP\bin\php\php5.3.10\ext\目录

2.      打开LAMP中Apache使用的php.ini文件

我的环境里是E:\WAMP\bin\apache\Apache2.2.21\bin\php.ini

注意不是LAMP中PHP的php.ini文件

定位到很多extension=php_xxxx.dll所在的位置,在末尾处添加extension=php_myhello.dll

3.      重新启动Apache

4.      回到[PHP Source]\ext\myhello目录

打开myhello.php文件,将如下代码注释掉

if (!extension_loaded('myhello')){

dl('php_myhello.' . PHP_SHLIB_SUFFIX);

}

注释的原因是dl()函数在PHP5中已经被删除了。其实不注释掉也没有问题。因为之前已经在php.ini中加载了php_myhello.dll,所以实际上程序执行不到dl('php_myhello.' . PHP_SHLIB_SUFFIX)。

5.      将修改好的myhello.php复制到LAMP的WEB目录中

我的环境里是E:\WAMP\www目录

6.      在浏览器里输入http://localhost/myhello.php

如果你看到如下信息,那么恭喜你,大功告成了!!!

Functions available in the test extension:

confirm_myhello_compiled

 

Congratulations! You have successfully modifiedext/myhello/config.m4. Module myhello is now compiled into PHP. Hello Cextension


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值