VC 建立php擴充
軟體 php的源檔案和安裝包要一致
php5.3.8(VC9 x86 Thread Safe)
php5.3.8源檔案(tar.bz2)
VC
bison.exe
MSYS(MSYS類似於Cygwin,但是由於工作原理的不同,速度更快、體積更小、功能強大、便於攜帶http://code.google.com/p/msys-cn/)
因為我的開發只是一個很簡單的demo,沒有使用第三方了類庫。如果是把linux下拿過來的擴充項目,可能用到一些庫。可能用cygwin會比較好。但是沒有cygwin完全可以在window下開發。
1。解壓下載到的源檔案包tar.bz2包到C盤c:/phpsrc,並且解壓php安裝包(VC9 x86Thread Safe,也就是能夠正常使用的php壓縮包檔案)到C:/php,我們只需要裡面的一個檔案C:/php/dev/ php5.lib,複製php5.lib到c:/phpsrc。
2。複製bison.exe到Microsoft Visual Studio\Common\MSDev98\Bin把Microsoft Visual Studio\Common\MSDev98\Bin的絕對路徑添加到windows環境變數
3.在這裡我們開始產生產生config.w32.h。CMD在裡面操作
進入:c:/phpsrc執行
C:\phpsrc>buildconfRebuilding configure.jsNow run 'configure --help'
?建立一個臨時環境變數
C:\phpsrc>set path=%path%;C:/phpsrc/binC:\phpsrc>cscript /nologo configure.js --with-php-build="../phpsrc" --without-libxml --disable-odbc
如果想要Non Thread Safe 模式就去掉上面的命令最後的參數--disable-zts
Saving configure options to config.nice.batChecking for cl.exe ... Detected compiler MSVC9 (Visual C++ 2008) Detected 32-bit compilerChecking for link.exe ... C:\Program Files\Microsoft Visual Studio 9.0\VC\BINChecking for nmake.exe ... Checking for lib.exe ... ...........中間省略.............-------------------------------------------| | |-------------------------------------------| Build type | Release || Thread Safety | Yes || Compiler | MSVC6 (Visual C++ 6.0) || Architecture | x86 |-------------------------------------------Type 'nmake' to build PHP
如果出現上類似的提示, 說明你的PHP開發環境已經搭建成功,同時在main下面多了一個 config.w32.h。即PHP開發環境的設定檔。如果沒有這個指令碼,windows下開發php,簡直太悲慘了。
?
4.開發PHP擴充的方法
到這裡我們就可以建立開發PHP擴充了,但是無從下手,我們該怎麼辦,其實PHP也給我們提供了很好用的工具來建立一個PHP擴充的骨架。就是用C:\phpsrc\ext下的ext_skel_win32.php如何運行,以php結尾,說明依賴php。下面看具體方法,我建立一個名為’test‘的擴充。這裡要確保你的php.exe可以直接在cmd下使用,具體還是加入Path路徑。雖然有ext_skel_win32.php,用php執行,但是我們不知道具體的內容。其實執行如下命令:
C:\phpsrc\ext>php ext_skel_win32.php'sh' 不是內部或外部命令,也不是可啟動並執行程式或批次檔。
ext_skel_win32.php源碼中說要使用cygwin,但我機器上沒有裝cygwin,另外發現其中實際上只使用到了sh,而我機器上裝的MSYS裡也有sh,應該可以用的吧,於是就將ext_skel_win32.php中的$cygwin_path變數設定成了MSYS的BIN目錄
$cygwin_path = 'c:\msys\1.0\bin';
?
C:\phpsrc\ext>php ext_skel_win32.phpext_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 compiledPHP Warning: fopen(/.php): failed to open stream: No such file or directory inC:\phpsrc\ext\ext_skel_win32.php on line 52C:\phpsrc\ext>
?可以看到如何使用這php指令碼。大體命令如下
c:\phpsrc\ext>php ext_skel_win32.php --extname=testCreating directory testCreating basic files: config.m4 config.w32 .svnignore test.c php_test.h CREDITS EXPERIMENTAL tests/001.phpt test.php [done].To use your new extension, you will have to execute the following steps:1. $ cd ..2. $ vi ext/test/config.m43. $ ./buildconf4. $ ./configure --[with|enable]-test5. $ make6. $ ./php -f ext/test/test.php7. $ vi ext/test/test.c8. $ makeRepeat steps 3-6 until you are satisfied with ext/test/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.c:\phpsrc\ext>
這樣就成功建立了一個php擴充骨架,你可以在裡面修改。具體的還要好好研究PHP API。具體擴充的位置就在ext目錄下,開啟可以看到test檔案夾,這就是剛才命令建立的。
C:\phpsrc\ext\test 的目錄2011-11-29 16:57 .2011-11-29 16:57 ..2011-11-29 16:57 16 .svnignore2011-11-29 16:57 1,970 config.m42011-11-29 16:57 282 config.w322011-11-29 16:57 4 CREDITS2011-11-29 16:57 0 EXPERIMENTAL2011-11-29 16:57 2,768 php_test.h2011-11-29 16:57 5,128 test.c2011-11-29 16:57 4,954 test.dsp2011-11-29 16:57 500 test.php2011-11-29 16:57 tests 9 個檔案 15,622 位元組 3 個目錄 7,441,883,136 可用位元組C:\phpsrc\ext\test>
這樣一個php擴充的架構已經建立完成了。
?
下面就是配置使用vc++6開發這個擴充
III. 添加依賴的php5ts.lib
? 在php的二進位包中的 dev目錄下將 php5ts.lib 拷到我們的test目錄中, 否則編譯將通不過。
IV. 添加test c代碼
產生的test目錄中有關鍵檔案包括
? test.dsp,
? test.c,
? php_test.h,
其他檔案暫時不必關心.
提示:切忌test目錄不可以挪移出ext目錄,否則會編譯報缺少php.h.
1. 修改php_test.h
擴充的新函數: 在PHP_FUNCTION(confirm_test_compiled); 行後添加一行
PHP_FUNCTION(confirm_test_compiled);PHP_FUNCTION(test); // 新增的行
2. 修改test.c
在PHP_FUNCTION(confirm_test_compiled) 後添加我們的新函數
PHP_FUNCTION(test){ php_printf("Hello C extension");}
在數組zend_function_entry test_functions[]增加一行
const zend_function_entry test_functions[] = {PHP_FE(confirm_test_compiled,NULL)/* For testing, remove later. */PHP_FE(test, NULL) // 新增的行PHP_FE_END/* Must be the last line in test_functions[] */};
?V. 構建DLL檔案
用vc6開啟我們的工程,就是test.dsp
1. 修改編譯方式為release: 選擇Build->Set Active Configuration設定預設編譯方式為Release, 否則會提示缺少php5ts_debug.lib ,其實就是php5ts.lib。
2. 按F5編譯。會在ext上級的Release_TS目錄下產生php_test.dll
提示:如果願意使用命令列編譯也是可以的,命令如下:
msdev test\test.dsp /MAKE "test - Win32 Release_TS"
?VI. 整合dll到php中。
?