Develop php extensions using c/c ++ in windows

Source: Internet
Author: User
: This article describes how to use cc ++ to develop php extensions in windows. For more information about PHP tutorials, see. I. required tools:

Cygwin is a unix-like simulated environment running on windows platforms.
Php source code (compressed package)
Php binary file, that is, php installed on windows
Vs2008 or higher (vs2010, vs2013)
Win32build.rarand bindlib_win32.rar (mainly used to generate config. win32.h)

II. configuration process

1: decompress the PHP source code. there is an ext directory in it. here is the directory responsible for developing PHP extensions. the directory contains all the default extension source code and two files,
Ext_skel_win32.php and ext_skel files
Ext_skel is an extended shell that cannot be run on windows. Therefore, cygwin must be used to simulate the unix environment.

2: After decompression, we need to generate our own extension directory in the ext directory.
Run win + r cmd to go to the command line. then run cd to jump to the ext directory and enter the following command:
Php ext_skel_win32.php-extname = myext or
Php.exe ext_skel_win32.php-extname = myext

Errors may occur here.
First, php and php.exeindicate that php.exe is installed in php on Windows, and the root directory of php.exe is used to parse the php file. Therefore, we need to set the installed php directory to the environment variable.
Second, if cygwin is not installed or cygwin is not set, the following error occurs. Therefore, we need to install cygwin, and then open ext_skel_win32.php in the ext Directory of the php source code. there is a variable named $ cygwin_path = "c:/cygwin/bin "; modify the directory you installed and save it. try again

If you still report an error, please refer to this figure to see if you have made a mistake.

After all the operations are completed, if the following conditions occur, the extension directory (skeleton) is successfully created,

The directory is like this. the two files that need to be modified are (as shown by the arrow)

3: Add the dependent php5ts. lib
Copy php5ts. lib under the dev directory in the php binary package to your extension directory.

4: (1) modify the php_hello.h file
Extend the new function: add a line of PHP_FUNCTION (hello) in PHP_FUNCTION (confirm_myhello_compiled );
(2) modify the hello. c file
Add a row 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) // New Row 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. }  4. 

5. build dll extension files
Next, compile the extension file and use vs2008 to open hello. DSPs, select the compilation method as Release_TS, and then start the compilation, or enter ext/hello through the command line to execute msdev hello. if dsp/MAKE "hello-Win32 Release_TS" is successful, php_hello.dll will be generated in the ext peer directory Release_TS.
At this point, the main task is basically completed, but it is also the easiest to make a mistake.
The most common error is that it will produce Cannot open include file "../main/config. win32.h" during compilation and the file will not be available in the main folder.
In this case, extract win32build.rarand bindlib_win32.rar to the win32/build directory of the php source code and add win32/build to the environment variable.
Go to cmd to go to the php source code directory and enter
Cscript/nologo configure. js-with-php-build = "../win32build"-without-libxml-disable-odbc may appear after you press enter

I didn't find cl.exe. Therefore, we need to add the bin directory in the installed vs2008 directory to the environment variable. on my machine, it is "D: \ vs2008 \ VC \ bin" and run it again, an error may still be reported. I cannot find the image .. This probably means the c ++ compiler is vc9.0 (vs2008) at least, so try to use compiler above vs2008

Continue execution:

This file can be found in the Microsoft Visual Studio 10.0 \ Common7 \ IDE directory and copied to the VC \ bin directory. The statement is successfully executed. Generate the config. win32.h file under the D: \ php source code \ main directory.

Open config. win32.h: find it.

If you use vc10 for compilation, you need to modify it

Finally solved the problem and compiled .. If yes, generate the dll,
However, I still encountered a compilation problem. fatal error c1902: The Program database manager does not match ..
It's really hard. Baidu found the same problem on the CSDN Forum and finally solved it like this.
Mspdbsrv.exe or mspdbcore. dll is lost or their versions are different from those of mspdb80.dll. make sure the same versions of the three files are installed in the system.
Copy the three files in the \ Common7 \ IDE directory under the VC installation directory to the VC \ bin directory under the installation directory.
After all the problems are solved, I have compiled php_hello.dll.

6: Finally, copy it to the ext directory under the binary php Directory, open php. ini, and add extension = php_hello.dll.
Restart apache and test your functions.

PS: The function I tested was cock ()

Output result:

This ends... It's really crazy to do this. I'm glad to patiently solve the problem multiple times and finally succeed. I posted all my problems for your reference. Php extension is fun. we can develop our own php functions. But the premise is that c/c ++ has a good learning experience... Let's take a look at it and wish you an early success ~

The above describes how to use c/c ++ to develop php extensions in windows, including some content. I hope my friends who are interested in PHP tutorials will be helpful.

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.