Compile PHP and an extension under Win(x86)

This guide is based on the following official php.net Wiki Page. This guide covers the compile process of

  • PHP (>= 5.5)
  • a PHP-extension (Pecl)

under Windows with Microsoft Visual Studio Express 2012 for Windows Desktop. As result of this operation we will get binaries of PHP for Windows as well as an extension (php_*.dll). We will use the

1
uprofiler

extension (php_uprofiler.dll) as example.

Requirements

Before we start with the compile process we need the following software, libraries, tools and dependencies for this guide:

Preparation / Setup

Install

1
Microsoft Visual Studio Express 2012 for Windows Desktop

. We do not need any special configuration. Just install in default setup.

We need to create directory

1
C:\php-sdk

with the following command

1
mdkir C:\php-sdk

As next step we need to extract the content of

1
php-sdk-binary-tools-20110915.zip

to the previously created directory

1
C:\php-sdk

. As result we have a folder structure like this:

1
2
3
C:\php-sdk\bin
C:\php-sdk\script
C:\php-sdk\share

In directory

1
bin

we will find (hopefully :) the files

1
phpsdk_setvars.bat

and

1
phpsdk_buildtree.bat

as well as some others. But these two files are important for the next step!

Afterwards

1
open VS2012 x86 Native Tools Command Prompt (it's available from the start menu)

and execute the following commands

1
2
3
4
C:
cd C:\php-sdk\
bin\phpsdk_setvars.bat
bin\phpsdk_buildtree.bat phpdev

Extract the

1
PHP sourcecode

to

1
C:\php-sdk\phpdev\vc11\x86\php-source-directory

. As result of this operation you should have a

1
win32

directory within the

1
C:\php-sdk\phpdev\vc11\x86\php-source-directory

directory (

1
C:\php-sdk\phpdev\vc11\x86\php-source-directory\win32

).

Extract the

1
PHP build dependencies

to

1
C:\php-sdk\phpdev\vc11\x86

. As result of this operation you should have a

1
deps

directory within the

1
C:\php-sdk\phpdev\vc11\x86

directory (

1
C:\php-sdk\phpdev\vc11\x86\deps

).

Compile PHP

To compile PHP we need to change the directory to the path of your PHP source code

1
cd C:\php-sdk\phpdev\vc11\x86\php-source-directory

Afterwards we run

1
buildconf

to prepare everything. It makes the configure command available and provide it with the current available flags for compiling. To get an overview of the available build/compiler flags you can use the command

1
configure --help

Based on this information create your configure command like this:

1
configure --disable-all --enable-cli --enable-$remains

Note: The

1
deps

should include the libraries needed to build most the core extensions. However, some other extensions may need additional libraries, header files and helper apps. See libs, fetch the version you need and extract the archive into the

1
deps

directory.

After you have configured how you want to build PHP, you can run

1
nmake

If you want the resulting PHP builds and extensions to be zipped, after

1
nmake

also run

1
nmake snap

The compiled PHP is now available under

1
C:\php-sdk\phpdev\vc11\x86\php-source-directory\Release_TS

. If you ran

1
nmake snap

the zip file will also be here. If you compiled with

1
–disable-zts

the compiled PHP will be under

1
C:\php-sdk\phpdev\vc11\x86\php-source-directory\Release

.

Recompile after changes

If we want to compile PHP again – after we did some changes for example – we need to clean up old compiled binaries, objects, deps first. This is done with the following commands.

Change to PHP source directory

1
cd C:\php-sdk\phpdev\vc11\x86\php-source-directory

and call the cleanup command

1
nmake clean

If you need to update the

1
configure

script

1
buildconf --force

Afterwards create your

1
makefile
1
configure --disable-all --enable-cli --enable-$remains

And now start compile again

1
nmake

Adding an extension (PECL and non-PECL)

Theoretically it doesn’t matter which type of extension you are about to compile. If it is not a PECL extension you will just need to do some extra steps. But in general the compile process is similar to a PECL extension. So for this example we are using the non-PECL extension

1
uprofiler

.

Navigate to the

1
x86

directory:

1
cd C:\php-sdk\phpdev\vc11\x86

Create a directory named

1
pecl

:

1
mkdir C:\php-sdk\phpdev\vc11\x86\pecl

Extract the previously downloaded archive

1
uprofiler-master.zip

in some temporary directory and put the content of the contained

1
extension

directory into

1
C:\php-sdk\phpdev\vc11\x86\pecl\uprofiler-X.Y.Z

where x,y and z are the current major (x), minor (y) and release (z) number. If you have downloaded an extensions sourcecode archive directly from PECL you will always find an versioned directory after extracting the archive. Extensions from other sources often does not have an already versioned directory. So we just create one by our own. At the time writing this tutorial

1
uprofiler

’s most recent version is 0.9.4 (Note: While it will say 0.10.5 when compiled!). So for this example we would create a directory

1
C:\php-sdk\phpdev\vc11\x86\pecl\uprofiler-0.9.4

Now change to PHP’s source directory

1
cd C:\php-sdk\phpdev\vc11\x86\php-source-directory

and call

1
buildconf

afterwards execute

1
configure --help

The output should now contain a

1
--enable-uprofiler

option.
Configure PHP to compile nothing but the

1
uprofiler

extension.

1
configure --disable-all --enable-cli --enable-uprofiler=shared

Note: We need to compile it „shared“ otherwise it will collide with already existing object

1
_getrusage

.

1
nmake

Test the binary with this command

1
php -m

to make sure

1
uprofiler

exists.

Utilities

Resource Hacker

This utility shows dependency information and other assembly information from a DLL. For instance, it shows which version of the Visual C++ Runtime the DLL was linked against.

We can get it here

Summary
Article Name
Compile PHP and an extension under Win(x86)
Description
This guide is based on the following official php.net Wiki Page. The guide covers the compile process of PHP (>= 5.5) and an PHP-extension (Pecl) under Windows with Microsoft Visual Studio Express 2012 for Windows Desktop.
Author
Publisher Name
phpfluesterer.de
Publisher Logo

3 Antworten zu “Compile PHP and an extension under Win(x86)”

  1. Elmartino sagt:

    Hi!

    I tried your article and it works great for the uprofiler extension. Now i wanted to use this for: https://github.com/eduardok/libsmbclient-php but unfortunately when i’m typing buildconf and then configure –help, the –enable-smbclient option is not listed.. Have you got any idea what can be the problem?

    Thanks in advance!

  2. Freddy sagt:

    I am trying to build my own PHP extension on Windows. I’m using this https://wiki.php.net/internals/windows/stepbystepbuild tutorial, which has helped me a lot, but during the compilation i got the error below:

    error LNK2019: simbolo externo __imp_QMCallx sin resolver
    And this same error for the rest of the functions i’m using. I’m trying to build this lib using the VS2012 x86 Native Tools Command Prompt, doing nmake over the PHP source directory, which should build the whole PHP solution (including it’s libs).

    This extension is needed to handle QM Database (www.openqm-zumasys.com) with PHP over Windows. This library is working fine on linux (CentOS).

    The source code to build this lib was downloaded from QM Client Downloads (http://www.openqm-zumasys.com/downloads/).

    I already have installed QM on my pc, so i guess all needed libraries are installed too.

    Those functions as QMCallx belong to qmclient library. I know the path and i have access to all these external libs.

    I’m just stuck at this point.

    If is needed posting the source code, please, let me know it and i’ll do it.

    Any comment? Thanks in advance.

  3. rishikesh chandan sagt:

    I am trying to compile PHP-7.0.11 and a sample extension on Windows 8.1 using Visual Studio 2015. I have followed every step but still getting the following error when i execute nmake command:

    bison.exe –output=Zend/zend_language_parser.c -v -d -p zend Zend/zend_language_parser.y
    zend/zend_language_parser.y:49: unrecognized: %code
    zend/zend_language_parser.y:49: Skipping to next %
    zend/zend_language_parser.y:52: unrecognized: %destructor
    zend/zend_language_parser.y:52: Skipping to next %
    zend/zend_language_parser.y:53: unrecognized: %destructor
    zend/zend_language_parser.y:53: Skipping to next %
    NMAKE : fatal error U1077: ‚“C:\Program Files\ux-utils\bison.exe“‚ : return code ‚0x1‘
    Stop.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

*