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.
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
Install from the network
##The C drive is installed by default Feel free to download the cache. Remember to delete it after installation. I put it on the desktop. 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 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. 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" Select C Select your php extension folder path here and name the project Select "Use visual studio" and select the project type "Dynamic Link Library (DLL) Project" will default to the next step until completed. #5. There will be many errors when you first open it. Let’s start configuring the project. First change the project solution configuration to Release Right-click the project properties, C/C, General, Additional Include directories, edit # 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-srcE:\vs_c \test\phpext\php-7.0.5-src\mainE:\vs_c \test\phpext\ php-7.0.5-src\TSRME:\vs_c \test\phpext\php-7.0.5-src\ZendRight-click project properties, C/C, preprocessor , preprocessor definition, edit, add the following variables: ZEND_DEBUG=0PHP_EXTENSIONPHP_WIN32ZEND_WIN32HAVE_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)
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)
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:
##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: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!