转载请注明文章出处:https://itlanyan.com/windows-compile-php72-extension/

准备工作

  1. https://github.com/Microsoft/php-sdk-binary-tools下载PHP-SDK(在右边的“clone or download”点击,选择下方的“download zip”);
  2. https://windows.php.net/downloads/releases/下载PHP7.2的源码,此时最新版本是7.2.8,选择“php-7.2.8-src.zip”下载;
  3. https://visualstudio.microsoft.com/zh-hans/downloads/选择“visual studio community 2017”,安装过程中选择C++功能。

编译

  1. 新建编译目录,例如“D:\PHP2Compile”;
  2. 将”php-sdk-binary-tools-master.zip”中的文件解压到编译目录下;
  3. 按住shift在编译目录内点击右键,选择“open powershell window here”;
  4. 执行”phpsdk-vc15-x64.bat”,成功后提示符从“>”变成”$”;
  5. 执行”phpsdk_buildtree phpdev”,成功后目录中会多一个“phpdev”目录,命令行的目录自动切换到”phpdev/vc15/x64″;
  6. 在”phpdev/vc15/x64″目录下新建php-src文件夹,将PHP源码复制到此目录;
  7. 切换到php-src目录(cd php-src),执行”phpsdk_deps -u”;
  8. 在“phpdev/vc15/x64”下建立pecl目录(与PHP源码目录同级),将拓展复制到该目录下;
  9. 在PHP源码目录内执行”buildconf”;
  10. 执行“configure –一些选项”命令配置编译选项,例如”configure –disable-all –enable-cli –enable-cgi –enable-zlib –enable-hash –enable-session –without-gd –with-bz2 –enable-yourext”;
  11. 执行nmake命令编译PHP及拓展。

编译成功后,在源码的X64目录下会生成“Release”或”Release_TS”目录,编译好的php.exe及生成的拓展dll均在此目录下。dll的文件名为php_xxxx.dll,例如”php_tlanyan.dll”。

TS和NTS

默认编译出来的拓展是TS(线程安全)的版本(位于Release_TS目录中),如果要编译非线程安全版本,configure时加入”–disable-zts”选项。

编译成功后,线程安全版本信息为(php.exe -v输出):

PHP 7.2.8 (cli) (built: Aug 14 2018 10:53:41) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

非线程安全版本输出:

PHP 7.2.8 (cli) (built: Aug 14 2018 11:47:40) ( NTS MSVC15 (Visual C++ 2017) x64 )                                      Copyright (c) 1997-2018 The PHP Group                                                                                   Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

注意事项

  1. 原PHP-SDK下载地址https://windows.php.net/downloads/php-sdk/只能下载PHP7.1及以前的SDK工具,PHP7.2的工具需从github上下载;
  2. 如果已经之前安装过visual studio,执行”phpsdk-vc15-x64.bat”出现”could not determine ‘vc15’ directory”的错误提示,表示未安装VC工具,启动安装工具添加VC支持即可;
  3. 编译前可将不需要的拓展删除,加快编译速度;
  4. 如果自定义的拓展出现“cannot open include file ‘win95nt.h’”,可使用这个头文件:https://github.com/nonylene/isucon5/blob/master/a/.local/php/include/php/main/win95nt.h

参考

  1. https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2
  2. http://blog.51cto.com/lancelot/2054953