If you're getting an error that says "We really need that arpa\nameser.h file - it is part of the win32build package", make sure you download a bindlib-something archive which is not found in VC9/x86 folder itself, but it is present in the VC9/x86/archive.
Building from source
This chapter teaches how to compile PHP from sources on windows, using Microsoft's tools. To compile PHP with cygwin, please refer to Installation on Unix systems.
This documentation will give you a basic understanding of how to compile PHP on Windows. This guide takes advanges of the many improvements made to the build system available in PHP 5.3.0 and greater.
Getting started
Before we can compile PHP we will need a compiler and some of the many libraries that PHP depends on. The only supported compilers on Windows is MSVC (Microsoft Visual C++). Versions 6 and greater is supported, however for better performance Visual C++ 2008 or greater is recommeded. This documentation will use Visual C++ 2008 and PHP 5.3.x as a base.
Visual C++ exists in two editions, a commercial edition (Visual Studio) and in an "Express" edition, which is freely available from Microsoft.
Installing using Visual Studio
When installing Visual Studio, you will be prompted to choose which components you wish to use, in here you must check the "Visual C++" component. If you plan on compiling x64 versions of PHP, you must select the x64 compilers component below "Visual C++".
If you already installed Visual Studio, then plugin your DVD and select "Add/Remove components", and follow the procedure above for installing the compiler.
Installing using Visual C++ Express
Visual C++ Express is available for free from Microsoft at: » http://www.microsoft.com/express/download/.
Before installing, the installation wizard will ask you to install additional components such as SQL Server, Silverlight and so on, none of these are required for compiling PHP.
Installing the Windows Platform SDK
Before PHP can be compiled, we need the Windows Platform SDK. By default Visual C++ comes with the Windows Platform SDK version 6.0A, this can however not be used due to incompatibility with previous versions of the SDK. So the Windows Platform SDK 2008, which is version 6.1 is required. The SDK is freely available from Microsoft at: » http://www.microsoft.com/downloads/details.aspx?FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc.
When downloading the SDK, choose the web install, as you will only need selected components. When the download is done and the setup is prompting you to select which components you require, then select the C++ compilers (remember to tick the x64 compilers, if you want to compile the 64bit version of PHP). You don't need any other tools such as CrystalReports, Mobile Development tools or anything, just the C++ libraries, headers and compilers.
Directory structure
Now we need to set a directory structure for where the PHP sources will be located, aswell as the default required libraries for compiling PHP. Below structure is simple and easy usable, its however not required to have it exactly like this, but its easy to overlook.
Example #1 Source and toolchain directory structure
C:. +---php | +---src <- Here will the PHP sources be located | +---win32build <- Toolchain and libraries for x86 (32 bit) versions | | +---bin <- Toolchain | | +---include <- Header files | | +---lib <- Library files | +---win64build <- Toolchain and libraries for x64 (64 bit) versions (optional) | +---bin <- Toolchain | +---include <- Header files | +---lib <- Library files
Getting the PHP source
There are serveral ways to get the PHP sources, either from the source tarballs or directly from the PHP SVN repository. This documentation will simply assume either option is choosen and the sources either have been exracted/checked out to the "C:\php\src\" directory from the above directory structure.
Getting the PHP toolchain
To compile PHP, we need a toolchain of different programs for compiling and linking default extensions. These are all available in a single zip file from the PHP on Windows repository: » http://pecl2.php.net/downloads/php-windows-builds/php-libs/binary-tools.zip.
Extract this zip file so it complies with the directory structure for the "C:\php\win32build\" directory. If you are planning to compile 64bit versions of PHP, you need to do the same with the "C:\php\win64build\" directory. This is alright as the tools are platform independent.
Getting the libraries required for PHP
PHP is a glue language, and depends on many libraries in order to function. By default PHP requires a set of libraries for compiling the core (Engine, CLI SAPI and the standard library).
These are available from PHP.net, organized in compiler versions and machine architectures. A standard PHP build requires the following mandatory libraries:
- ICU
- iconv (libiconv)
- libxml2
- zlib
If you plan to compile a standard versions of PHP. Meaning compiling all default enabled extension by running configure without any arguments, You will also need the following additional libraries for GD:
- FreeType
- libpng
- libjpeg
All these libraries are packed and published to the following URL: » http://pecl2.php.net/downloads/php-windows-builds/php-libs/.
On the above location, libraries are organized by which compiler that were used to compile them and on which platform architecture. Below explains the different shot name variants for each compiler.
- VC6 - Visual C++ 6.0
- VC8 - Visual C++ 2005
- VC9 - Visual C++ 2008
When you have downloaded the mandatory libraries, and/or any additional libraries you may require, then exact them respectedly to the directory structure set in "C:\php\winXXbuild\", so headers are located in the include directory and library files in the libs directory.
Setting up the environment
Before we can compile PHP, we must alter the PATH environment variable so the configure script can pickup the toolchain in the "C:\php\winXXbuild\bin\" directories. To do this, open the Start menu and right click on "Computer" and select "Properties". Select the "Advanced" tab, and hit the button saying "Environment variables". In the global variables section, find the PATH variable and select "Edit". Append the following to its current value: ;C:\php\win32build\bin and for 64bit support: ;C:\php\win32build\bin;C:\php\win64build\bin.
Now you must create the following directory structure on the drive that you are using to compile on:
Example #2 bison.simple directory structure
C:. +---usr | +---local | +---share
Now copy the "C:\php\win32build\bin\bison.simple" file into the "C:\usr\local\share" directory.
Compiling
Now PHP is ready to be compiled, open the Start menu, choose "Programs" and find "Microsoft Visual Studio" (or "Microsoft Visual C++"), select the tools folder and open "Visual Studio 2008 Command Prompt". This will open a console window saying "Setting up environment for XXX", where XXX may be either x86 or x64. If you wish to compile the 64bit version of PHP, you must execute the following:
Example #3 Setting up the x64 build environment
cd bin vcvarsx86_amd64
Now cd to the "C:\php\src\" directory and execute the buildconf script like so:
Example #4 Running buildconf
buildconf Now run 'configure --help'
This means that the configure script was generated with success. Now we need to run the generated configure script to generate the Makefiles for the compiler. Simply run configure like so:
Example #5 Running configure
configure ... ... ...
As buildconf advertised, then configure --help can be used to see available commands for configure. If you have multiple cores available then enable multi processing, which optimizes and makes the compilation faster. Multi processing can be enabled using the --enable-one-shot option for configure like so:
Example #6 Enabling multi processing in configure
configure --enable-one-shot ... ... ...
If you wish to do a minimal build, then you need to disable all default enabled extensions using the --disable-all option. When disabling all extensions and SAPIs, you must select atleast one before you can compile a PHP binary. Below example will disable all extensions, enable the CLI SAPI and enable multi processing:
If you need to export debugging symbols and doing a debug build, you also need to enable the following two options: --enable-debug and --enable-debug-pack.
Example #7 Configuring a minimal build of PHP with multi processing
configure --disable-all --enable-cli --enable-one-shot ... ... ...
If you need to compile an NTS (NOT Thread Safe) build of PHP, you need to pass the --disable-zts option to configure. By default PHP is compiled with Thread Safety.
Now we are ready actually perform the compile, this is done by executing nmake, like so:
Example #8 Executing nmake
nmake ... ... ...
Testing the build
When the build process is complete, the PHP binary, along with all the compiled extensions will be located in a sub directory to the sources directory. Depending on the build type, it will be located in one of the following directories:
- Debug - Debug, NOT Thread Safe
- Debug_TS - Debug, Thread Safe
- Release - Release, NOT Thread Safe
- Release_TS - Release, Thread Safe
If you are compiling the 64bit version of PHP, these directories will be located inside another directory called x64 from the root of the sources.
To test the compiled binary, then cd to its directory and execute the following:
Example #9 Testing the compiled PHP binary
C:\php\src\Release_TS\> php -v PHP 5.3.X (cli) (built: XXX XX XXXX XX:XX:XX) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
Building from source
07-Mar-2010 10:52
27-Feb-2010 10:12
To build everything in the distribution, pass --enable-snapshot-build to configure.js. In addition, you may like to use "snap" as the nmake target. If you intend to build the PHP Windows Installer, WiX source files can be obtained from http://svn.php.net/repository/php/win-installer/.
17-Aug-2009 06:10
PHP extensions can be compiled statically or shared.
- Shared compilation creates a well-known php_*.dll file.
- Static compilation puts the extension directly into PHP (therefore it does not need to be loaded and cannot be unloaded)
You can switch whether to compile given extension statically or as library by adding =static or =shared to the extension in configure.js command during the compilation.
configure --enable-http=static --with-openssl=shared
// http extension will be included in PHP
// openssl extension will be compiled as separate DLL
12-Mar-2009 02:50
If trying to compile the code (Windows XP, SDK v6.1) and you get the following types of errors:
c:\phpdev\php-5.2.9\main\php_network.h(128) : warning C4005: 'POLLIN' : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1495) :
see previous definition of 'POLLIN'
c:\phpdev\php-5.2.9\main\php_network.h(129) : warning C4005: 'POLLPRI' : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1496) :
see previous definition of 'POLLPRI'
c:\phpdev\php-5.2.9\main\php_network.h(130) : warning C4005: 'POLLOUT' : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1499) :
see previous definition of 'POLLOUT'
c:\phpdev\php-5.2.9\main\php_network.h(131) : warning C4005: 'POLLERR' : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1502) :
see previous definition of 'POLLERR'
c:\phpdev\php-5.2.9\main\php_network.h(132) : warning C4005: 'POLLHUP' : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1503) :
see previous definition of 'POLLHUP'
c:\phpdev\php-5.2.9\main\php_network.h(133) : warning C4005: 'POLLNVAL' : macro redefinition
c:\program files\microsoft sdks\windows\v6.1\include\winsock2.h(1504) :
see previous definition of 'POLLNVAL'
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN
\cl.exe"' : return code '0x2'
Stop.
You can alter the Makefile and add the following switch to CFLAGS:
/D _WIN32_WINNT
07-Dec-2007 12:14
If you are getting the following error message:
Input Error: There is no script engine for file extension ".js"
with Visual Studio 2005 Command Prompt or similar, try to add "/e:jscript" to the command line.
Something like:
cscript /nologo /e:jscript win32/build/buildconf.js
It worked for me, I hope it helps.
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
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!
>> Compiling using Visual Studio .NET will create binaries dependent of msvcp71.dll
Only if you compile with the wrong runtime library.
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
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.
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!
