Home  >  Article  >  Backend Development  >  Using c/c++ to develop php extensions under windows

Using c/c++ to develop php extensions under windows

WBOY
WBOYOriginal
2016-08-08 09:29:041162browse

1: Required tools:

cygwin, which is a Unix-like simulation environment running on the Windows platform
php source code (compressed package)
php binary file, which is 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 code. There is an ext directory in it. This is the directory responsible for developing PHP extensions. In the directory There are default source codes for all extensions and two files,
ext_skel_win32.php and ext_skel files
ext_skel is a shell for creating extensions and cannot run on Windows, so cygwin must be used to simulate a Unix environment

2: After decompression, we need to generate our own extension directory in the ext directory.
We directly enter the command line with win+r cmd, then 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

It’s easy to make mistakes here
First of all: php and php.exe refer to php installed on Windows. There is php.exe in its root directory, which is used to parse php files, so we need to set the installed php directory to the environment variable.
Secondly: If cygwin is not installed or cygwin is not set up, the following error will occur. 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 called $cygwin_path = “c:/cygwin/bin”; change it to the directory you installed and save it, try again

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

If the following situation occurs after everything is completed, it means that the extension directory (skeleton) was successfully created. The directory contained in

probably looks like this, and the two files we need to modify are (as shown by the arrows)

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

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

<code>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. };  
</code>

Add our new function after PHP_FUNCTION(confirm_myhello_compiled)

<code>1. PHP_FUNCTION(hello){  
2.     php_printf(”Hello C extension”);  
3. }  
4. 
</code>

5: Build the dll extension file
The next step is to compile the extension file. Open hello.dsp with vs2008, select the compilation mode as Release_TS, and then start compilation, or enter ext/hello through the command line and execute msdev hello.dsp /MAKE "hello - Win32 Release_TS" If successful, Then php_hello.dll will be generated in Release_TS, the same level directory of ext.
At this point, the main tasks are basically completed, but this is also where errors are most likely to occur.
The most common error is that Cannot open include file "../main/config.win32.h" will appear during compilation. As a result, this file does not exist in the main folder.
Therefore, unzip both win32build.rar and bindlib_win32.rar to the win32/build directory in the PHP source code, and add win32/build to the environment variable
Enter cmd to jump to the php source code directory and enter
cscript /nologo configure.js –with-php-build=”../win32build” –without-libxml –disable-odbc It may appear after pressing Enter

means that cl.exe is not found, so we need to Add the bin directory in the installed vs2008 directory to the environment variable. On my machine it is "D:vs2008VCbin". If you execute it again, an error may still be reported. I can't find the picture. . The general meaning is that the c++ compiler is vc9.0 (vs2008) at least, so try to use a compiler of vs2008 or above

Continue execution:

This file can be found in the Microsoft Visual Studio 10.0Common7IDE directory, copy it to the VCbin directory. Can. Continue executing the statement successfully. Generate the config.win32.h file in the D:php source code main directory.

Open config.win32.h: Find inside

If you use vc10 to compile, you need to modify it to

Finally solved the problem, compiled. . If successful, generate dll,
But I still encountered compilation problems, fatal error c1902: The program database manager does not match. .
It was really hard. I found the same problem on the CSDN forum through Baidu, and finally solved it like this.
mspdbsrv.exe or mspdbcore.dll is missing or its version is different from that of mspdb80.dll. Make sure the same version of these three files is installed in the system.
Copy these three files from the Common7IDE directory under the VC installation directory to the VCbin under the installation directory
All the problems were solved, and I successfully compiled php_hello.dll

6: Finally, we copied it to the ext directory under the binary php directory, opened php.ini, and added extension=php_hello.dll
Restart apache and test your functions.

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

Output result:

This is the end. . . It was really crazy to work on this thing. Fortunately, I patiently tried to solve the problem many times and finally succeeded. I have posted all the problems I encountered for your reference. PHP extensions are quite fun, we can develop our own PHP functions. But the premise is that you have to learn c/c++ well. . . Anyway, take your time and wish everyone success~

The above introduces the use of c/c++ to develop PHP extensions under Windows, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP cachingNext article:PHP caching