从源程序编译

本章讲述了在 Windows 下如何使用 Microsoft 的工具编译 PHP。要在 CygWin 中编译 PHP,请参考 Unix 系统下的安装一章。

需求

要编译 PHP 需要 Microsoft 开发环境。推荐使用 Microsoft Visuaul C++ 6.0,不过 Visual C++ .NET 版也可以用。自 PHP 5 起也支持免费的 Microsoft .NET toolchain(需要安装 Windows Platform SDK,Visual C++ Toolkit 和 .NET Framework SDK)。

要释放下载的文件还需要一个 ZIP 文件的解压缩工具。Windows XP 和更新的版本中已经内置了此功能。

在开始之前,需要先下载:

最后,还需要 PHP 本身的源代码。可以通过匿名 CVS 得到最新的开发版本,一个快照或者最新发行版本的源程序的打包文件。

放到一起

下载了所有的包后需要将它们解压缩到适当的位置:

  • 建立一个工作目录作为解压缩后存放所有文件的地方,例如:C:\work

  • 在工作目录(C:\work)下新建一个目录 win32build 并将 win32build.zip 解压缩到其中。

  • 在工作目录(C:\work)下新建一个目录 bindlib_w32 并将 bindlib_w32.zip 解压缩到其中。

  • 将下载的 PHP 源程序解压缩到工作目录(C:\work)中。

  • 要编译库还需要(或下载现成的)头文件和库文件并分别放置到 C:\work\win32build\includeC:\work\win32build\lib 目录中去。

  • 如果没有安装有 bison 和 flex 的 Cygwin,那还需要将 C:\work\win32build\bin 目录放入系统 PATH 中,以使此工具可以被配置脚本找到。

经过以上步骤后目录结构应该如下:
+--C:\work
|  |
|  +--bindlib_w32
|  |  |
|  |  +--arpa
|  |  |
|  |  +--conf
|  |  |
|  |  +--...
|  |
|  +--php-5.x.x
|  |  |
|  |  +--build
|  |  |
|  |  +--...
|  |  |
|  |  +--win32
|  |  |
|  |  +--...
|  |
|  +--win32build
|  |  |
|  |  +--bin
|  |  |
|  |  +--include
|  |  |
|  |  +--lib

如果不是用 Cygwin 的话,还必须新建一个目录 c:\usr\local\lib 并将 bison.simplec:\work\win32build\bin 拷贝到 c:\usr\local\lib 中。

注意: 如果想使用 PEAR 以及舒适的命令行安装程序,就非要用 CLI-SAPI。有关 PEAR 和安装程序的更多信息见 PEAR 网站。

编译 resolv.lib

必须编译 resolv.lib 库。自己决定是需要调试信息(bindlib - Win32 Debug)还是不需要(bindlib - Win32 Release),但是要记住自己的选择,因为用 Debug 模式编译的 resolv.lib 只能和 Debug 模式编译的 PHP 连接。编译适当的配置:

  • 图形界面用户,通过鼠标双击 C:\work\bindlib_w32\bindlib.dsw 运行 VC++。接着选择 Build => Rebuild All。

  • 命令行用户,确认要么注册了 C++ 环境变量,要么运行了 vcvars.bat,然后运行下面任意一行命令:

    • msdev bindlib.dsp /MAKE "bindlib - Win32 Debug"

    • msdev bindlib.dsp /MAKE "bindlib - Win32 Release"

到这一步,应该得到了一个可用的 resolv.libc:\work\bindlib_w32\Debug 或者 Release 目录下。将此文件拷贝到 c:\work\win32build\lib 目录下并覆盖同名的文件。

用新编译系统编译 PHP(仅适用于 PHP >=5)

本节讲解了怎样用新编译系统编译 PHP >=5,此系统是基于 CLI 的,并且非常类似于 PHP 的 Unix 编译系统。

注意: 此编译系统在 PHP 4 中不可用。请参考用 DSW 文件编译 PHP一节。

开始之前要确保阅读了放到一起一节并且已经编译了所有所需的库,例如 LibxmlICU(PHP >= 6 中需要)。

首先应该打开一个用于 Visual Studio 的命令行提示符,应在开始菜单中能找到。正常的命令提示符可能不能用,因为没有设定所需的环境变量。输入类似 cd C:\work\php-5.x.x 的命令进入 PHP 源程序目录。现在可以开始配置 PHP 了。

第二步是运行 buildconf 批处理文件来扫描文件夹寻找 config.w32 来建立配置脚本。默认时此命令还会搜索以下目录:pecl; ..\pecl; pecl\rpc; ..\pecl\rpc。自 PHP 5.1.0 起,可以通过 --add-modules-dir 参数来改变此行为(例如 cscript /nologo win32/build/buildconf.js --add-modules-dir=../php-gtk2 --add-modules-dir=../pecl)。

第三步是配置。要查看可用的配置选项列表,输入 cscript /nologo configure.js --help 命令。选择了需要激活或禁止的选项之后,输入类似 cscript /nologo configure.js --disable-foo --enable-fun-ext 的命令。使用 --enable-foo=shared 将尝试将“foo”扩展库编译为共享的,可动态加载的模块。

最后一步是编译。只需运行命令 nmake 即可。所生成的文件(例如 .exe 和 .dll)将会放在 Release_TSDebug_TS 目录下(如果编译的是线程安全版的话),或者是 ReleaseDebug 目录。

作为可选项还可以运行 PHP 的测试套件,输入 nmake test 命令。如果要做一个特定的测试,可以用 'TESTS' 变量(例如 nmake /D TESTS=ext/sqlite/tests test 将只执行 sqlite 测试)。要删除编译过程中生成的文件,可以用 nmake clean 命令。

有个很有用的选项来编译快照:--enable-snapshot-build。此选项添加了一个新的编译模式(nmake build-snap)。这将尝试编译每一个可用的扩展库(默认为共享模块方式),但是忽略个别的扩展库或 SAPI 中的编译错误。

用 DSW 文件编译 PHP(PHP 4)

自 PHP 5 起不支持用 DSW 文件来编译 PHP,因为有了一个更灵活的系统。不过还是可以用此方法,但要记住此系统并不经常维护,因此编译可能会有问题。不过这是在 Windows 下编译 PHP 4 的唯一方法。

配置 MVC ++

第一步是配置 MVC++ 来准备编译。启动 Microsoft Visual C++,在菜单中选择 Tools => Options。在对话框中选择 directories 标签。按顺序把下拉选项选为 Executables,Includes 和 Library files。其内容应类似于:

  • Executable files:C:\work\win32build\bin,Cygwin 用户:C:\cygwin\bin

  • Include files:C:\work\win32build\include

  • Library files:C:\work\win32build\lib

编译

对于初学者最好的上手方法是编译 CGI 版本:

  • 图形界面用户,运行 VC++,选择 File => Open Workspace 菜单并选择 C:\work\php-4.x.x\win32\php4ts.dsw。接着选择 Build => Set Active Configuration 菜单并选择想要的配置,要么是 php4ts - Win32 Debug_TS 要么是 php4ts - Win32 Release_TS。最后选择 Build => Rebuild All。

  • 命令行用户,确认要么注册了 C++ 环境变量,要么运行了 vcvars.bat,然后从 C:\work\php-4.x.x\win32 目录下运行下面任意一行命令:

    • msdev php4ts.dsp /MAKE "php4ts - Win32 Debug_TS"

    • msdev php4ts.dsp /MAKE "php4ts - Win32 Release_TS"

    • 到这一步,应该得到一个可用的 php.exeC:\work\php-4.x.x.\Debug_TSRelease_TS 目录下。

有可能通过编辑 main/config.win32.h 文件对编译做少量的定制。例如可以改变 php.ini 文件的默认位置,内置的扩展库,以及扩展库的默认位置。

下一步可能想要编译 CLI 版本,这是设计用于 PHP 的命令行方式。步骤和编译 CGI 版本相同,只除了要选择 php4ts_cli - Win32 Debug_TS 或者 php4ts_cli - Win32 Release_TS 项目文件。成功编译后会在 Release_TS\cli\ 或者 Debug_TS\cli\ 目录下找到 php.exe 文件。

要编译 SAPI 模块(php4isapi.dll)来将 PHP 和 Microsoft IIS 集成起来,将 active configuration 设为 php4isapi-whatever-config 并编译所需要的 dll。.


add a note add a note User Contributed Notes
giunta dot gaetano at gmail dot com
27-Jun-2007 01:43
A detailed tutorial for building php 5 using VC++ 2005 express edition (ie. the free version) is available here: http://elizabethmariesmith.com/2006/11/15/

It can be a huge time saver if you do not have a complete VC6 toolchain already setup
axel at kollmorgen dot net
27-May-2007 01:13
>>Compiling using Visual Studio .NET will create binaries dependent of msvcp71.dll
>
> Only if you compile with the wrong runtime library.

if this is so easy, please tell us how you managed to *not* create dependencies on msvcp71.dll/msvcr80/... using Visual C++ .NET 2000/2003/2005. thanks!
10-Dec-2006 02:09
>> Compiling using Visual Studio .NET will create binaries dependent of msvcp71.dll

Only if you compile with the wrong runtime library.
zbowden at vt dot edu
12-May-2006 02:12
When running "cscript /nologo configure.js" 5.x on Windows
2000 or XP, if you get the following error:
Microsoft JScript runtime error: Object doesn't support this
property or method

You need to have at least version 5.6 of WSH.
This is necessary because configure.js will use the .Exec()
method of the WScript.Shell.
This evidently didn't exist until 5.6?
 
You can download WSH 5.6 here:
http://www.microsoft.com/downloads/details.aspx?
FamilyID=c717d943-7e4b-4622-86eb-95a22b832caa&DisplayLang=en
C-Reaction.net - Software Development
03-Apr-2006 07:16
Don't forget to setup Environment variable:
BISON_SIMPLE="C:\\work\\win32build\\bin\\bison.simple"

or this can cause error:
c:/usr/local/lib/bison.simple: No such file or directory

Alexey Furmanov.
26-Jan-2005 12:30
Compiling using Visual Studio .NET will create binaries dependent of msvcp71.dll which has three repercussions:
1. This dll will need to be distributed.
2. The binaries are not currently compatible with the Zend optimiser (not sure why it should make a difference but it does).
3. Binaries are larger!