在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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章