Fork me on GitHub

在Windows上编译PHP及PECL扩展

在Windows上编译PHP及扩展


编译之前

在Windows上编译PHP需要3件事:

  • 正确的编译环境,包括编译器、PHP SDK、还有编译用到的工具
  • 预编译的类库和头文件,在本地编译第三方扩展时会用到
  • PHP源码

编译器

要求

PHP官方仅支持Microsoft's Visual C++与MinGW,更多编译器信息查看支持列表,以下是支持的VC版本:

  • Visual C++ 9.0 (Visual Studio 2008 or Visual C++ 2008) for PHP 5.4.
  • Visual C++ 11.0 (Visual Studio 2012) for PHP 5.5 or 5.6.
  • Visual C++ 14.0 (Visual Studio 2015) for PHP 7.0+.

如果使用VC9,你还需要Windows SDK 6.1(在这里查看额外信息)

设置

  • 编译 PHP 5.4:

    • 安装 Visual Studio 2008
    • 安装 Windows SDK 6.1
  • 编译 PHP 5.5或5.6:
    安装 Visual Studio 2012。如果使用Express版,安装Visual Studio 2012 Express for Windows Desktop
  • 编译 PHP 7.0+:
    安装 Visual Studio 2015

命令行工具

每个版本的Visual Studio(或Windows SDK)都提供了用于编译的命令行工具(从Windows开始菜单打开或者从VS的bin目录里打开)
  • 如果编译 PHP 5.4 打开“Windows SDK 6.1 shell”并执行以下命令:

    setenv /x86 /xp /release

  • 如果编译 PHP 5.5 或 5.6 打开“VS2012 x86 Native Tools Command Prompt(本机工具命令提示)
  • 如果编译 PHP 7.0+ 打开“Developer Command Prompt for VS2015”

TIPS: 后面所有的命令都应在命令行工具里执行


下载编译所需资源

  1. 获取PHP源码,有两种方式:

    • download a source archive (Stable releases, QA releases)
    • clone the PHP Git repository (GitHub mirror)
  2. 获取工具:

  3. 获取PHP依赖的库文件:

    • download the packaged deps from http://windows.php.net/downloads/php-sdk/
    • dependency archives are named deps-<php version>-<vc version>-<architecture>.7z
    • chose the archive which matches your desired PHP version and architecture, for example deps-5.6-vc11-x86.7z

Note: This should include the libraries needed to build most the core

  1. 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 deps directory (see below).


设置编译目录

这个操作在编译PHP之前只需操作一次就够了.

  1. Create the build directory c:php-sdk
  2. Unpack the binary tools archive into this directory, it should contain three sub-directories: bin, script and share
  3. Open the command prompt and enter the build directory:

    cd c:php-sdk
    
  4. Run the buildtree batch script which will create the desired directory structure:

    binphpsdk_buildtree.bat phpdev
    
  5. The buildtree script hasn't been updated for newer versions of VC++ so:

    • If compiling for VC11: copy C:php-sdkphpdevvc9 to C:php-sdkphpdevvc11
    • If compiling for VC14: copy C:php-sdkphpdevvc9 to C:php-sdkphpdevvc14
  6. Extract the PHP source code to C:php-sdkphpdevvc##x##, where:

    • vc## is the compiler version you are using (vc9, vc11 or vc14)
    • x## is your architecture (x86 or x64)
    • For example: C:php-sdkphpdevvc11x86php-5.6.4-src
  7. In the same directory where you extracted the PHP source there is a deps directory. Here you will need to extract the libraries required to build PHP, which you downloaded in the perevious step (the deps-*.7z archive).

编译

  1. Open the command prompt and enter the build directory:

    cd c:php-sdk

  2. Set up the build environment variables:

    binphpsdk_setvars.bat

  3. Change directory to the location of your PHP source code, e.g.

    cd C:php-sdkphpdevvc11x86php-5.6.4-src

  4. Run:

    buildconf

  5. To get an overview of the compiling flags:

    configure --help

  6. Create your configure command:

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

  7. To build PHP, run:

    nmake

  8. If you want the resulting PHP builds and extensions to be zipped, after 'nmake' also run:

    nmake snap

  9. The compiled PHP is now under “C:php-sdkphpdevvcXXx86php-source-directoryRelease_TS”. If you ran 'nmake snap' the zip file will also be here.

    > If you compiled with “–disable-zts” the compiled PHP will be under “C:php-sdkphpdevvcXXx86php-source-directoryRelease”
    

当做出修改后重新编译

  1. Clean up old compiled binaries

    nmake clean

  2. If you need to update the 'configure' script

    buildconf --force

  3. Create your makefile: see release

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

  4. Compile

    nmake

编译 PECL 扩展

Many PECL extensions already have prebuilt DLLs which can be downloaded from the extension page on the PECL web site. For example, on the APCu extension page, there are DLL links next to the release archive link. They lead you to a page where you can download the compiled extension for your desired PHP version and architecture.

To build a PECL extension (for example APCu):

  1. Open the extension's page on PECL ( APCu)
  2. Download the extension source either by:

    • downloading a source archive
    • fetching the source from the extension's repository (link can be found under Browse Source)
  3. Create a directory named pecl on the same level as your PHP source directory, e.g. C:php-sdkphpdevvc11x86pecl
  4. Extract or clone the extension source code to the pecl directory

    • if cloning, clone to a subdirectory, e.g. C:php-sdkphpdevvc11x86peclapcu
    • source code archive should already contain a subdirectory named e.g. apcu-4.0.7
  5. Open a command prompt, run the setvars script, and enter your PHP source directory
  6. Rebuild the configure script by running:

    buildconf
    
  7. Executing configure –help should now contain an option to enable APCu

    --enable-apcu    Whether to enable APCu support
    
  8. Configure and build:

    configure --disable-all --enable-cli --enable-apcu=shared
    //这里加上“=shared”就会编译为DLL,否则扩展会被编译进PHP
    
    nmake
  9. Test the binary with a php -m command, to make sure APCu is loaded

At this point, your compiled extension will be located in the release directory.

Additional Links:


LDAP Support Notes

Add “–with-ldap” to your configure

Make sure to package your generated binaries with libsasl.dll, which will not be built through this process - you can find it inside your C:php-sdkphp53devvc9x86depsin folder, or you can download it from one of the PHP official binary packages


Pitfalls

fatal error C1900: [] mismatch between 'P1' version '200801116' and 'P2' version '20070207'

If you are using Visual Studio 2008 Express and Platform SDK 6.1 make sure you have the latest versions and check for version incompatibilities. There are known issues with old versions of VS 2008 Express and Platform SDK 6.1.

The order in which you install the Visual Studio 2008 Express Edition and Platform SDK 6.1 is important due to the issue reported in Windows SDK for Windows Server 2008 and .NET Framework 3.5 Overview, Windows SDK for Server 2008 (v6.1) overwrites VS2008 SP1 CRT files and Installing Windows SDK for Server 2008 (v6.1) after VS2008 SP1 causes conflicts with Security Update (KB971092). Install the SDK first, with any of its service packs and security updates before installing Visual Studio 2008 Express Edition.

Make sure that you are running setenv /xp /x86. For example, setenv /xp /x64 or setenv /vista /x64 causes the above error message even if you are using Visual Studio 2008 Express SP1 and Platform SDK 6.1 (60001.18000.367) on a Vista 64bit system.


Utilities

Resource Hacker
Utility to show 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.

Get it here: http://angusj.com/resourcehacker/

internals/windows/stepbystepbuild.txt· Last modified: 2015/07/12 11:35 byihabunek

标签: php, pecl

仅有一条评论

  1. [...]来源:http://www.crarun.com/article-16.html[...]

添加新评论