Developing PHP extensions with VS

Source: Internet
Author: User

Original: Developing PHP extensions with VS

Pre-development preparation work:

VS (I used 2013)

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

IIS7.5 with PHP running environment (for testing)

PHP compiled after the program and the source code before compiling, I use the current version of the latest 5.6.9 (: http://windows.php.net/download/)

Compiled program path: E:\php-5.6.9-nts-Win32-VC11-x86\

Pre-compiled source path: E:\php-5.6.9-src\

Steps:

1. Installing Cygwin

Installing from the Network

Install C drive By default

Download cache random, install the end remember to delete, I was put on the desktop

In China, choose Http://mirrors.63.com, followed by the next to the end.

2. Find the PHP source directory my is (E:\PHP-5.6.9-SRC, the following use this representative source directory), open E:\php-5.6.9-src\ext\ext_skel_win32.php

This is changed to your Cygwin installation directory, my is the C drive, so do not change.

3. Run cmd, enter E:\php-5.6.9-src\ext\, run Php.exe ext_skel_win32.php--extname=test, here test represents your PHP extension .

Open E:\php-5.6.9-src\ext\ you will see a test folder, this is your extension.

4. Open vs Select "file"--"new"--"Create Directory from existing code"

Select C + +

Choose your PHP extension folder path, and give the project a name

Select Use Visual Studio, project type select dynamic link library (DLL) project, followed by default next until complete.

5. There will be a lot of errors at the beginning of the opening and we'll start configuring the project.

Change the project solution configuration to release first

Right-click Project Properties, c/C + +, general, additional include directories, editing

Add the following PHP source directories (The actual directory is based on the developer's own directory):

E:\php-5.6.9-src
E:\php-5.6.9-src\main
E:\php-5.6.9-src\TSRM
E:\php-5.6.9-src\Zend

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

Zend_debug=0
Php_extension
Php_win32
Zend_win32
Have_TEST=1 ( red section here, to change to your extension name, do not change to your extension, PHP will not recognize )
Compile_dl_TEST(The red part here, to change to your extension name, do not change to your extension, PHP will not recognize )
ZTS ( this variable plus is thread safe to turn on, not to turn off thread safety )

Build solution, error message shows that "config.w32.h" is not found, search "config.w32.h" in the source code file directory, find "config.w32.h.in" in E:\php-5.6.9-src\win32\build\ folder , copy the file to the E:\php-5.6.9-src\main\ folder and remove the ". In" from the back

Build the solution again and display the error message LNK1120

ERROR 7 lnk1120:5 an unresolved external command E:\php-5.6.9-src\ext\test\Release\phptest.dll 1 1 phptest

Right-click Project properties, connectors, input, additional dependencies, edit, Php5.lib path (this file in the PHP compiled program folder, the root directory of the Dev folder)

Open test.c

Find this section of code:

php_function (confirm_test_compiled) {Char*arg =NULL; intArg_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 to PHP.","Test", ARG); Return_stringl (STRG, Len,0);}

change confirm_test_compiled to test_echo

Find this section of code again:

Const zend_function_entry test_functions[] = {    Php_fe (confirm_test_compiled,    NULL)         /*  */    php_fe_end    /**/}; 

and change the confirm_test_compiled into test_echo .

Build the solution, find your 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 new test.php file inside

<?  Echo test_echo ("123");

Run to get results:

This Test_echo function is our own custom function, and you can develop your own extensions to improve PHP performance as needed.

Developing PHP extensions with VS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.