Home  >  Article  >  Backend Development  >  How to develop PHP7 extensions with VS2015 (vs14)

How to develop PHP7 extensions with VS2015 (vs14)

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-22 09:18:141978browse

This article will introduce to you how to use VS2015 (vs14) to develop PHP7 extensions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to develop PHP7 extensions with VS2015 (vs14)

Preparation work before development:

VS (I use 2013)

Cygwin (Download address: http://www. cygwin.com/)

Equipped with IIS7.5 of PHP running environment (used for testing)

php compiled program and pre-compiled source code, I am using the latest version 7.0. 5 (Download address: http://windows.php.net/download#php-7.0)

Compiled program path: E:\vs_c \test\phpext\php-7.0.5-src

Source code path before compilation: E:\vs_c \test\phpext\php-7.0.5-nts-Win32-VC14-x86\

Steps:

1. Install Cygwin

How to develop PHP7 extensions with VS2015 (vs14)

Install from the network

How to develop PHP7 extensions with VS2015 (vs14)

##The C drive is installed by default

How to develop PHP7 extensions with VS2015 (vs14)

Feel free to download the cache. Remember to delete it after installation. I put it on the desktop.

How to develop PHP7 extensions with VS2015 (vs14)

How to develop PHP7 extensions with VS2015 (vs14)

In China, just choose http ://mirrors.163.com, follow the next step until the end.

2. Find the php source code directory mine is (E:\vs_c \test\phpext\php-7.0.5-src, this will be used below to represent the source code directory), open E:\vs_c \test\phpext \php-7.0.5-src\ext\ext_skel_win32.php

How to develop PHP7 extensions with VS2015 (vs14)

Change this to your cygwin installation directory. Mine is the C drive, so there is no need to change it.

3. Run cmd, enter E:\vs_c \test\phpext\php-7.0.5-src\ext\, run php.exe ext_skel_win32.php --extname=test, here test represents you php extension.

How to develop PHP7 extensions with VS2015 (vs14)

Open E:\vs_c \test\phpext\php-7.0.5-src\ext and you will see a test folder. This is your extension.

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

How to develop PHP7 extensions with VS2015 (vs14)

Select C

How to develop PHP7 extensions with VS2015 (vs14)

Select your php extension folder path here and name the project

How to develop PHP7 extensions with VS2015 (vs14)

Select "Use visual studio" and select the project type "Dynamic Link Library (DLL) Project" will default to the next step until completed.

How to develop PHP7 extensions with VS2015 (vs14)

#5. There will be many errors when you first open it. Let’s start configuring the project.

How to develop PHP7 extensions with VS2015 (vs14)

First change the project solution configuration to Release

How to develop PHP7 extensions with VS2015 (vs14)

Right-click the project properties, C/C, General, Additional Include directories, edit

How to develop PHP7 extensions with VS2015 (vs14)

# and add the following PHP source directories (the actual directory is subject to the developer's own directory):

E:\vs_c \ test\phpext\php-7.0.5-src

E:\vs_c \test\phpext\php-7.0.5-src\main

E:\vs_c \test\phpext\ php-7.0.5-src\TSRM

E:\vs_c \test\phpext\php-7.0.5-src\Zend

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

ZEND_DEBUG=0

PHP_EXTENSION

PHP_WIN32

ZEND_WIN32

HAVE_

TEST=1 (The red part here needs to be changed to your extension name. If you don’t change it to your extension name, php will not recognize it)

COMPILE_DL_

TEST (Here The red part needs to be changed to your extension name. If you don’t change it to your extension name, PHP will not recognize it)

ZTS (Adding this variable turns on thread safety, not adding it turns off thread safety. You can judge whether to add this variable based on whether the php you compiled is thread safe. ps: I suffered a loss because of not responding. PHP does not recognize the extension)

How to develop PHP7 extensions with VS2015 (vs14)

Generate the solution. The error message shows that "config.w32.h" cannot be found. Search for "config.w32.h" in the source code file directory. h", find "config.w32.h.in" in the E:\vs_c \test\phpext\php-7.0.5-src\win32\build\ folder, and copy this file to E:\vs_c \test \phpext\php-7.0.5-src\main\ folder, remove the following ".in"

Generate the solution again, and the error message LNK1120

error 7 error LNK1120: 5 unresolved external commands E:\vs_c \test\phpext\php-7.0.5-src\ext\test\Release\phptest.dll 1 1 phptest

right-click project properties, connector, enter , additional dependencies, edit, put the path to php5.lib (this file is in the program folder after php is compiled, in the dev folder of the root directory)

Note: In order to allow the extension to work with php The running environment matching depends on the compiled version of your php running environment (php7.0.5 is compiled by VC14), which is the config in the E:\vs_c\test\phpext\php-7.0.5-src\main\ folder. Add to the w32.h file:

#define PHP_COMPILER_ID "VC14"

Open E:\vs_c \test\phpext\php-7.0.5-src\ext\test\test.c

Find this paragraph 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);
}

Change confirm_test_compiled to test_echo

Find this code again:

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 confirm_test_compiled inside to test_echo

To generate a 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, and create a new test.php file in it

<?php 
echo test_echo("123");

Run and get the result:

How to develop PHP7 extensions with VS2015 (vs14)

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

Recommended learning:

php video tutorial

The above is the detailed content of How to develop PHP7 extensions with VS2015 (vs14). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete