Development of PHP Extensions in C + + with Windows

Source: Internet
Author: User
Tags php source code rar
A: The required tools:

Cygwin, which is a UNIX-like simulation environment running on the Windows platform
PHP Source code (compressed package)
PHP binaries, which are the PHP installed on Windows
vs2008 or higher (vs2010,vs2013)
Win32build.rar and Bindlib_win32.rar (mainly used to generate config.win32.h)

Two: Configuration process

1: Unzip the PHP source, there is an ext directory, here is responsible for the development of PHP extension directory, the directory has the default all the extension source, there are two files,
Ext_skel_win32.php and Ext_skel files
Ext_skel is the shell that created the extension and cannot run on windows, so there must be Cygwin to emulate the UNIX environment

2: After extracting, we need to generate our own extension directory in the Ext directory.
We directly win+r cmd into the command line, then the CD jumps to the Ext directory, enter the following command:
PHP Ext_skel_win32.php–extname=myext or
Php.exe Ext_skel_win32.php–extname=myext

It's easy to make mistakes here.
First of all: PHP and Php.exe refers to the installation of PHP on Windows, its root directory has Php.exe, the purpose is to parse the PHP file, so we need to set the installed PHP directory into the environment variables.
Second: If Cygwin is not installed or Cygwin is not set, the following error will occur. So, we need to install Cygwin, and then open the PHP source code ext Directory ext_skel_win32.php, there is a variable called $cygwin_path = "C:/cygwin/bin"; Modify it to the directory you installed and save it, try again

If you still have an error, please look at this picture to see if you made a mistake.

After everything is done, the creation of the extended directory (skeleton) succeeds if the following conditions occur,

It contains a directory that looks like this, where we need to modify the two files (as shown by the arrows)

3: Add dependent Php5ts.lib
Copy the php5ts.lib from the dev directory in the PHP binary package to your extension directory

4: (1) Modify the Php_hello.h file
Extend new function: Php_function (confirm_myhello_compiled); Add a row after line php_function (hello);
(2) Modify the hello.c file
Add a line to the array zend_function_entry myhello_functions[]

1. zend_function_entry myhello_functions[] = {  2.     PHP_FE(confirm_myhello_compiled,    NULL)        /* For testing, remove later. */  3.     PHP_FE(hello, NULL) // 新增的行  4.     {NULL, NULL, NULL}    /* Must be the last line in myhello_functions[] */  5. };  

Add our new function after Php_function (confirm_myhello_compiled)

1. PHP_FUNCTION(hello){  2.     php_printf(”Hello C extension”);  3. }  

5: Build DLL Extension file
The next step is to compile the extension file, open hello.dsp with vs2008, choose Compile as Release_ts, then start compiling, or go to Ext/hello from the command line to execute Msdev hello.dsp/make "Hello-win32 Release_ts "If successful, the Php_hello.dll will be generated in the Ext sibling directory release_ts.
Basically the main task is completed here, but it is also the most error prone.
The most common error that is compiled when it appears is cannot open include file ". /main/config.win32.h "Results to the main folder there is no such file.
So, Win32build.rar and Bindlib_win32.rar are extracted into the PHP source Win32/build directory, and add Win32/build to the environment variable
Go to cmd to jump to PHP source directory, enter
Cscript/nologo configure.js–with-php-build= ". /win32build "–without-libxml–disable-odbc may appear after a carriage return

It means that there is no cl.exe, so we need to add the bin directory of the installed VS2008 directory to the environment variable, my machine is "D:\vs2008\VC\bin", again execution, there may still be error, figure I can not find. Probably means the C + + compiler is vc9.0 (vs2008) at least so try to use vs2008 compiler above

Continue execution:

This file can be found in the Microsoft Visual Studio 10.0\common7\ide directory and copied to the VC\bin directory. Continue executing the statement successfully. Generate Config.win32.h files under the D:\php source \main directory.

Open config.win32.h: Find inside

If you compile with VC10, you need to modify it to

Finally solve the problem, compile. If successful, build the DLL,
But I still met the compile problem, fatal error C1902: The program Database Manager does not match.
Really bitter, through Baidu, in the CSDN forum found the same problem, and finally the solution.
Mspdbsrv.exe or Mspdbcore.dll is missing or the version and Mspdb80.dll versions are different, make sure the same version of the three files is installed in the system.
In the VC installation directory under the \Common7\IDE directory to copy these three files to the installation directory under the VC\bin can be
All problems solved, I also succeeded in compiling the Php_hello.dll

6: Finally, we copy it to the EXT directory in the binary PHP directory, open the php.ini, add the Extension=php_hello.dll
Restart Apache to test your own functions.

PS: The function I tested at the time was cock ()

Output Result:

To this end ... This thing is really crazy, fortunately, patience to solve the problem many times to try, the ultimate success. I put all the problems I have encountered for your reference. PHP extension This thing is fun, we can develop our own PHP functions. But the premise is that C + + to learn very well ... In short, come slowly, I wish you all early success ~

The above introduces the development of PHP in C/D + + in Windows, including the content, I hope that the PHP tutorial interested in a friend to help.

  • Related Article

    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.