Home >Backend Development >PHP Tutorial >Introduction to error-free version of php calling c interface_PHP tutorial

Introduction to error-free version of php calling c interface_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:15741browse

1. First, you need to install PHP

2. Enter the ext directory under the PHP download and decompression directory
#cd /root/php-5.3.6/ext
#./ext_skel --extname=hmc

Explanation:
./ext_skel --extname=module_name
module_name is the name of the extension module you can choose, for example, I chose it hmc. After executing the tool, a directory with the module_name of your choice will be automatically created in the ext directory. Relevant codes have been generated in it. For these codes, you only need to adjust the three lines of comments in the config.m4 file to compile normally with this customization. Extension module for PHP.

3. Modify config.m4
You can copy the generated module directory to any place you want to place it
Enter the hmc directory
vi config.m4
Use a text editor to open the config.m4 file
Change
dnl PHP_ARG_WITH(hmc, for hmc support,
dnl Make sure that the comment is aligned:
dnl [ --with -hmc Include hmc support])
Modify to
PHP_ARG_WITH(hmc for hmc support,
Make sure that the comment is aligned:
[ --with-hmc Include hmc support])
Or change
dnl PHP_ARG_ENABLE(hmc, whether to enable hmc support,
dnl Make sure that the comment is aligned:
dnl [ --enable-hmc Enable hmc support])
to
PHP_ARG_ENABLE(hmc, whether to enable hmc support,
Make sure that the comment is aligned:
[ --enable-hmc Enable hmc support])

Generally I will choose the latter, then save and exit.

4. Modify the .c and .h file codes
Vi hmc.c
Modify the following code in the file
/* Every user visible function must have an entry in hmc_functions[].
*/
const zend_function_entry hmc_functions[] = {
PHP_FE(say_hello, NULL) /* Add a line of code*/
PHP_FE(confirm_hmc_compiled, NULL) / * For testing, remove later. */
{NULL, NULL, NULL} /* Must be the last line in hmc_functions[] */
};

Add the following code at the end of the file
PHP_FUNCTION(say_hello)
{
zend_printf("hello worldn");
}
Save the file and exit

vi php_hmc.h
In the file PHP_FUNCTION(confirm_hmc_compiled); add the following code before the line
PHP_FUNCTION(say_hello);
Save the file and exit

5. Compile and install
#phpize
#./configure --enable-hmc
#make
#make install
should be in the modules directory of php There is a hmc.so file below, the location will be different on different machines



6. Modify the PHP configuration
Edit php.ini and add the extension:
Add under the [PHP] module:
extension = hmc.so
Restart Httpd
#service httpd restart

7.php calls
to create a c.php file in the corresponding www directory
say_hello( ; #php –q c.php
Needs to enter the current www directory

To check whether the module is installed, you can use phpinfo()



You can also use the php -m command
First use

#which php

to find the directory where php is located, the directory is different on different machines
#/usr/ bin/php -m





9. Function with parameters

Vi hmc.c
Modify the content of the last say_hello function as follows: PHP_FUNCTION(say_hello)
{
zval ** yourname;

if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE)

{WRONG_PARAM_COUNT;
}
zend_printf("hello world, %sn" , Z_STRVAL_PP(yourname));
}

Modify c.php to
say_hello("Qingqing Yue'er");
?>



http://www.bkjia.com/PHPjc/740212.html


www.bkjia.com

true

http: //www.bkjia.com/PHPjc/740212.html

TechArticle1. First, you need to install PHP 2. Enter the ext directory under the PHP download and decompression directory #cd /root /php-5.3.6/ext #./ext_skel --extname=hmc Description: ./ext_skel --extname=module_name module_name is...
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