Window下php扩展开发环境搭建参考
一、工具准备
1、php sourcecode
2、php zip
3、phpsdk
http://windows.php.net/downloads/php-sdk/
4、vs 2008 /2013
版本对应说明
https://wiki.php.net/internals/windows/compiler
5、cygwin
注:开发环境php的版本要与 vc工具对应 以防生成扩展后无法载入
二、部署
一)目录结构
E:\PHP_WIN
├─php-5.4.36-nts-Win32-VC9-x86
│
├─php-5.4.36-src
│
└─win32build
note:win32build(php-sdk-binary-tools)
二)配置PATH
vc、 sdk 对应的bin路径添加到 path中。
或者直接使用附带工具(开始目录中可以找到)
VisualStudio 2008 Command Prompt | VS2013 x86 Native Tools Command Prompt
三)命令尝试
1.cl.exe
尝试运行,无报错即可。
E:\ProgramFiles\Microsoft Visual Studio 12.0\VC\bin>
2.sh.exe
1)MicrosoftVisual Studio 2013 SDK
http://www.microsoft.com/en-us/download/details.aspx?id=40758
三、建立开发环境
1、E:\PHP_WIN\php-5.4.36-src>buildconf
Rebuildingconfigure.js
Nowrun 'configure --help'
2、E:\PHP_WIN\php-5.4.36-src>cscript/nologo configure.js --with-php-build="e:\php_win\win32build"--without-libxml --disable-odbc
3、会生成 main 目录 里面有编译时需要引入的头文件。
四、建立PHP扩展框架
1、E:\PHP_WIN\php-5.4.36-src\ext>php ext_skel_win32.php
2、配置$cygwin_path
ext_skel_win32.php $cygwin_path = 'e:\cygwin64\bin'; 配置路径到 cygwin安装路径
3、E:\PHP_WIN\php-5.4.36-src\ext>php ext_skel_win32.php --extname=test
------------------------------------------------------------------
------------------------------------------------------------------
Creatingdirectory big
Creatingbasic files: config.m4 config.w32 .svnignore big.c php_big.h CREDITSEXPERIMENTAL tests/00
.phptbig.php [done].
Touse your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/big/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-big
5. $ make
6. $ ./sapi/cli/php -f ext/big/big.php
7. $ vi ext/big/big.c
8. $ make
Repeatsteps 3-6 until you are satisfied with ext/big/config.m4 and
step6 confirms that your module is compiled into PHP. Then, start writing
codeand repeat the last two steps as often as necessary.
------------------------------------------------------------------
------------------------------------------------------------------
4、会看到 test目录 其中是所需的框架
五、扩展开发结合vc
一)创建项目
1、文件-新建-从现有代码创建项目。
2、选择visual-c++,
3、找到项目位置E:\PHP_WIN\php-5.4.36-src\ext\test
4、项目名称php_test,
5、项目类型:选择“动态链接库dll项目”。
二)配置参数
菜单-项目-属性,打开php_test属性页对话框,
1、在“配置属性"-"链接器"中(linker->input)
1)vc12.0
E:\ProgramFiles\Microsoft Visual Studio 12.0\VC\lib\oldnames.lib
E:\ProgramFiles\Microsoft Visual Studio 12.0\VC\lib\msvcrtd.lib
E:\php_win\php-5.4.36-nts-Win32-VC9-x86\dev\php5.lib
C:\Program Files (x86)\MicrosoftSDKs\Windows\v7.1A\Lib\Kernel32.lib
2)vc9.0
C:\ProgramFiles\Microsoft Visual Studio 9.0\VC\lib\oldnames.lib
C:\ProgramFiles\Microsoft Visual Studio 9.0\VC\lib\msvcrtd.lib
E:\php_win\php-5.4.36-nts-Win32-VC9-x86\dev\php5.lib
C:\ProgramFiles \Microsoft SDKs\Windows\v7.1A\Lib\Kernel32.lib
3)注意:
*路径中 如有空格 请用 “C:\Program Files(x86)\Microsoft Visual Studio 9.0\VC\lib\oldnames.lib”包含路径。
*php5.lib
要根据扩展名称而定,记得要大写。
这里要去掉:“从父级或项目默认设置继承”的勾选
2在“配置属性"-"C/C++"
1)“常规”:附加包含目录:
C:\vc\vc2008compiler\PlatformSDK\Include
C:\vc\vc2008compiler\include
E:\PHP_WIN\win32build\include
E:\PHP_WIN\win32build
E:\PHP_WIN\php-5.4.36-src
E:\PHP_WIN\php-5.4.36-src\win32
E:\PHP_WIN\php-5.4.36-src\main
E:\PHP_WIN\php-5.4.36-src\TSRM
E:\PHP_WIN\php-5.4.36-src\Zend
2)“预处理器”:预处理器定义
WIN32
_DEBUG
_WINDOWS
_USRDLL
PHP_TEST_EXPORTS
ZEND_DEBUG=0
COMPILE_DL_TEST
ZTS
ZEND_WIN32
PHP_WIN32
HAVE_TEST=1
三)生成dll
1、右击php_test项目,选择“生成”
2、位置..\test\debug\ php_test.dll
四)测试
1、载入扩展后 运行E:\PHP_WIN\php-5.4.36-src\ext\test test.php
phptest.php
2、查看phpinfo();是否扩展的相关信息
六、问题
1、F:\php_win\php-5.3.8>buildconf 没有文件扩展“.js”的脚本引擎
没有文件扩展“.js”的脚本引擎
这样的错误,原因是因为JS扩展名的文件被其他软件关联了,需要取消关联。
如系统中安装了ULTRAEDIT或者E钻加密软件等,就需要去掉关联;
或者如下解决方法:
在运行中输入“regedit”进入注册表,
只需要把[HKEY_CLASSES_ROOT]".js" 项下的那个默认值改成 "JSFile" 就可以正常运行JS 文件了。
2、MicrosoftJScript 运行时错误: 'configure_module_dirname' 未定义
清楚 src文件 重新解压
3、'sh' 不是内部或外部命令,也不是可运行的程序或批处理文件。
$cygwin_path= 'e:\cygwin64\bin';
4、使用 VS2013x86 Native Tools Command Prompt
'-h'is not recognized as an internal or external command,
operableprogram or batch file.
NMAKE: fatal error U1077: '"' : return code '0x1'
'-h'不是内部或外部命令,也不是可运行的程序
或批处理文件。
NMAKE: fatal error U1077: “"”: 返回代码“0x1”
5、安装完vc,运行php ,如果报告批量 php扩展无法载入错误,php脚本无法执行,尝试重新启动服务器环境。
PHPWarning: PHP Startup: curl: Unable toinitialize module\nModule compiled with module API=20100525\nPHP compiled with module API=20131226\nTheseoptions need to match\n in Unknown on line 0
PHPWarning: PHP Startup: gd: Unable toinitialize module\nModule compiled with module API=20100525\nPHP compiled with module API=20131226\nTheseoptions need to match\n in Unknown on line 0