从源程序编译

开始之前,很值得回答一下这个问题:为什么在 Windows 下编译这么难?两个原因:

  1. Windows 下还没有愿意免费共享代码的开发人员群体。直接结果就是没有足够的投资去建立支持这种开发方式的体系。大体上,尽量得到的可用资源都是从 Unix 下的工具来的。不要奇怪这种传统不会时出现。

  2. 下面几乎所有的说明都是“看过就忘”的类型。所以坐稳当并且尽可能忠实地按照说明来做。

需求

要编译 PHP 需要 Microsoft 开发环境。推荐使用 Microsoft Visuaul C++ 6.0。要释放下载的文件还需要一个解压缩工具(例如:Winzip)。如果还没有解压缩的工具,可以从 InfoZip 下载一个免费版本。

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

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

放到一起

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

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

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

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

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

经过以上步骤后目录结构应该如下:

+--c:\work
|  |
|  +--bindlib_w32
|  |  |
|  |  +--arpa
|  |  |
|  |  +--conf
|  |  |
|  |  +--...
|  |
|  +--php-4.x.x
|  |  |
|  |  +--build
|  |  |
|  |  +--...
|  |  |
|  |  +--win32
|  |  |
|  |  +--...
|  |
|  +--win32build
|  |  |
|  |  +--bin
|  |  |
|  |  +--include
|  |  |
|  |  +--lib

新建一个目录 c:\usr\local\lib。将 bison.simplec:\work\win32build\bin 拷贝到 c:\usr\local\lib

注: Cygwin 用户应省略上一步。正确安装的 Cygwin 环境已强制提供了 bison.simplebison.exe 文件。

配置 MVC ++

下一步是配置 MVC ++ 来准备编译。运行 Microsoft Visual C++,在菜单中选择 Tools => Options。在对话框中,选择 directories 标签。依次将下拉框改为 Executables,Includes 和 Library files。其看上去应该是:

  • Executable files: c:\work\win32build\bin, Cygwin users: cygwin\bin

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

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

编译 resolv.lib

必须编译 resolv.lib 库。自己决定是需要调试信息(bindlib - Win32 Debug)还是不需要(bindlib - Win32 Release)。编译适当的配置:

  • 图形界面用户,运行 VC++,选择 File => Open Workspace,找到 c:\work\bindlib_w32 目录并选择 bindlib.dsw 文件。接着选择 Build => Set Active Configuration 菜单并选择需要的配置。最后选择 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 目录下并覆盖同名的文件。

编译

对于初学者最好的上手方法是编译 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 文件。

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

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


add a note add a note User Contributed Notes
zbowden at vt dot edu
12-May-2006 10: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 03: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 08: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!