PHP7 VS2015 compilation (extended development)

  • php7 requires vc2015


Visual Studio 2015 Community Edition

PHP -sdk-binary-tools-20110915.zip ( http://windows.php.net/downloads/php-sdk/ download)
deps-7.0-vc14-x86 ( http://windows.php .net/downloads/php-sdk/ download)
php-7.0.2 ( http://php.net/downloads.php download)

 

first step

  • Unzip the binary package of php-sdk-binary-tools, for example, I unzip it to my D:\vcmyprojects\php-sdk folder, the current directory structure is as follows


D:\vcmyprojects\php-sdk
    --bin
    --script
    --share

 

Then, this is that you have installed visual studio 2015. Open the VS2015 developer command prompt. Note that you must use this to compile. Ordinary cmd does not work. There are many detours. . .

 

  •  #Enter the directory

cd D:\vcmyprojects\php-sdk
  • #Set environment variables

bin \ phpsdk_setvars.bat
  • Edit the c:\php-sdk\php-sdk-binary-tools\bin\phpsdk_buildtree.bat file and
    add it before GOTO EXIT

MD %_%\vc14\x86\deps\binMD %_%\vc14\x86\deps\libMD %_%\vc14\x86\deps\includeMD %_%\vc14\x64\deps\binMD %_%\vc14\x64\deps\libMD %_%\vc14\x64\deps\include
  • #Create commonly used php-sdk directories

bin\phpsdk_buildtree.bat phpdev
  • Unzip deps-7.0-vc14-x86.7z to the D:\vcmyprojects\php-sdk\phpdev\vc14\x86\deps folder to cover, inside are all the library files we need and some necessary tools and so on.

  • Install cygwin, I use the latest x86 version, download and install it to c:\cygwin, so there is no need to modify ext_skel_win32.php

Compile and install php

Back to the VS2015 developer command prompt

  • #Enter the php source directory folder

cd D:\vcmyprojects\php-sdk\phpdev\vc11\x86\php-7.0.2buildconf

 

  • #View the expansion and compilation commands of the belt

configure --help
  • If you don't have php installed, here is now to help you, you can compile and install php first

configure --disable-all --enable-cli


Then, you will see Type'nmake' to build PHP, and then compile

nmake

The php.exe file is generated under the folder D:\vcmyprojects\php-sdk\phpdev\vc11\x86\php-7.0.2\Release_TS, and this path is added to the environment variable so that the php command can be used in the command line .

Develop the first extension of PHP

  • Enter the extension directory and generate an extension folder

cd D:\vcmyprojects\php-sdk\phpdev\vc11\x86\php-7.0.2\extphp ext_skel_win32.php --extname=raintest1


At this time, we can see our directory raintest1 in D:\vcmyprojects\php-sdk\phpdev\vc11\x86\php-7.0.2\ext, open raintest1\php_raintest1.h,

Open ext\ and you will see a test folder, this is your extension.

  •  Open VS and select "File"-"New"-"Create Directory from Existing Code"

  • Choose C++

  • Choose your php extension folder path here, and give the project a name

  • Select "Use visual studio", select "Dynamic Link Library (DLL) Project" as the project type, and then default to the next step until completion.

  • There will be many errors at the beginning, we will start to configure the project.

  • First change the project solution configuration to Release

  • Right-click project properties, C/C++, general, additional include directories, edit

Add the following php source code directories (the actual directory is subject to the developer's own directory):

E:\php-xxx-srcE:\php-xxx-src\mainE:\php-xxx-src\TSRME:\php-xxx-src\Zend

Right-click the project properties, C/C++, preprocessor, preprocessor definition, edit, and add the following variables:

ZEND_DEBUG=0PHP_EXTENSIONPHP_WIN32ZEND_WIN32HAVE_XXX=1COMPILE_DL_XXXZTS Note that the above XXX should be changed to uppercase extension (if the extension is called tonyenc, change XXX to TONYENC), otherwise PHP will not recognize the extension. ZTS is used to tell the compiler to enable thread safety (if it is removed, it is not enabled). Note that whether thread safety is enabled or not depends on the previously downloaded C:\php7-Win32-VC14-x64-ts, which also enables thread safety compilation, so thread safety is enabled here.

  • In the "Connector"-"Input" column on the left, in the "Additional Dependent Projects", add: C:\php7-Win32-VC14-x64-ts\dev\php7ts.lib, which is the compiled version downloaded earlier PHP program.

  • Click OK, and then select "Generate"-"Generate Solution" from the menu. Congratulations, the compiler reported an error: Cannot open the include file ../main/config.w32.h, then change C:\php7-Win32-VC14 Copy -x64-ts\win32\build\config.w32.h.in to C:\php7-Win32-VC14-x64-ts\main\config.w32.h (note that there is no in behind), then in config Add in .w32.h:

#define PHP_COMPILER_ID "VC14"

This will indicate that the runtime library is VC14, which matches the compiled PHP program downloaded earlier, and regenerate the solution, so that it can be successfully compiled!

 

Open test.c

Find this piece of code:

Copy code

PHP_FUNCTION(confirm_test_compiled)
{
    char * arg = NULL;
    int arg_len, len;
    char * strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
        return;
    }

    len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "test", arg);
    RETURN_STRINGL(strg, len, 0);
}

Copy code

Change confirm_test_compiled to test_echo

Find this piece of code:

const zend_function_entry test_functions[] = {
    PHP_FE(confirm_test_compiled,    NULL)        /* For testing, remove later. */
    PHP_FE_END    /* Must be the last line in test_functions[] */
};

Change the confirm_test_compiled inside to test_echo

Generate the solution, find your own php extension phptest.dll in the Release folder of the project root directory, copy it to the ext folder of php, and configure it in php.ini:

extension=phptest.dll

Restart IIS, create a new site, create a test.php file in it

<?php 
echo test_echo("123");

Run to get the result:

This test_echo function is our own custom function. You can also develop your own extensions to improve the performance of PHP according to your needs.

Reprinted at: https://my.oschina.net/colin86/blog/897008

Guess you like

Origin blog.csdn.net/an17822307871/article/details/112798421