How to compile PHP extensions in C language

Source: Internet
Author: User
Tags zts
This article provides a detailed analysis of PHP extensions written in C language. For more information, see 1: Pre-defined
In the home directory, you can also write a file in any other Directory, such as caleng_module.def.
The content is the name and parameters of the function you want to define:
Int a (int x, int y)
String B (string str, int n)

2: go to the ext Directory of the php source code directory
# Cd/usr/local/php-5.4.0/ext/

Run the command to generate the corresponding extension directory
#./Ext_skel -- extname = caleng_module -- proto =/home/hm/caleng_module.def

3: modify config. m4
Remove dnl comments

PHP_ARG_ENABLE (caleng_module, whether to enable caleng_module support,
Make sure that the comment is aligned:
[-Enable-caleng_module Enable caleng_module support])

4: modify caleng_module.c

The code is as follows:


/* {Proto int a (int x, int y)
*/
PHP_FUNCTION ()
{
Int argc = ZEND_NUM_ARGS ();
Int x;
Int y;
Int z;
If (zend_parse_parameters (argc TSRMLS_CC, "ll", & x, & y) = FAILURE)

Return;
Z = x + y;
RETURN_LONG (z );
}
/*}}}*/
/* {Proto string B (string str, int n)
*/
PHP_FUNCTION (B)
{
Char * str = NULL;
Int argc = ZEND_NUM_ARGS ();
Int str_len;
Long n;
Char * result;
Char * ptr;
Int result_length;

If (zend_parse_parameters (argc TSRMLS_CC, "sl", & str, & str_len, & n) = FAILURE)
Return;
Result_length = str_len * n;
Result = (char *) emalloc (result_length + 1 );
Ptr = result;
While (n --){
Memcpy (ptr, str, str_len );
Ptr + = str_len;
}
* Ptr = '\ 0 ';
RETURN_STRINGL (result, result_length, 0 );
}
/*}}}*/


5. generate an Extension Library
# Cd./caleng_module
#/Usr/local/php/bin/phpize
#./Configure -- with-php-config =/usr/local/php/bin/php-config
# Make
# Make install



6: corresponding extensions directory to php
As shown in
# Cd/usr/local/php/lib/php/extend/no-debug-non-zts-20100525/
The generated caleng_module.so file is in the directory.

7: modify php. ini
If the php. ini file cannot be found, you can view the phpinfo () file.
# Cd/usr/local/php/lib/
Added extension information for php. ini.
Extension = caleng_module.so

8: restart Apache
#/Usr/local/apache2/bin/apachectl restart

9: check loading
/Usr/local/php/bin/php-m

10: PHP call

The code is as follows:


Echo a (1, 2 );


Output 3 indicates the operation is successful!
======================================
Below is the original text
Develop PHP extensions using C in Linux
First download the PHP source package, assuming the source package directory is:/software/php-5.2.13
First download the PHP source package, assuming the source package directory is:/software/php-5.2.13
#> Cd/software/php-5.2.13/ext
2. Suppose we want to develop an extension named caleng_module, which contains two functions: a -- processing two integers and B-processing string repeated output;
1. First, write a function definition file. in this file, the prototype suffix of the function is def, which is assumed to be caleng_module.def.
Int a (int x, int y)
String B (string str, int n)
2. the extension directory caleng_module will be automatically created under the ext Directory through the extension skeleton generator.
#>./Ext_skel -- extname = caleng_module -- proto = caleng_module.def
3. modify the configuration file: #> vim/software/php-5.2.13/ext/caleng_module/config. m4, remove the comment label "dnl" in the following line, after the modification is as follows:
PHP_ARG_ENABLE (myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[-- Enable-myfunctions Enable myfunctions support])
4. improve functions of functions a and B: #> vim/software/php-5.2.13/ext/caleng_module/caleng_module.c
PHP_FUNCTION ()
{
Int x, y, z;

Int argc = ZEND_NUM_ARGS ();

If (zend_parse_parameters (argc TSRMLS_CC, "ll", & x, & y) = FAILURE)
Return;
Z = x + y;
RETURN_LONG (z );
}
PHP_FUNCTION (B)
{
Char * str = NULL;
Int argc = ZEND_NUM_ARGS ();
Int str_len;
Long n;
Char * result;
Char * ptr;
Int result_length;

If (zend_parse_parameters (argc TSRMLS_CC, "sl", & str, & str_len, & n) = FAILURE)
Return;
Result_length = str_len * n;
Result = (char *) emalloc (result_length + 1 );
Ptr = result;
While (n --){
Memcpy (ptr, str, str_len );
Ptr + = str_len;
}
* Ptr = '\ 0 ';
RETURN_STRINGL (result, result_length, 0 );
}
3. Compile and install php. assume that the installation directory of php is/usr/localhost/webserver/php.
#> Cd/software/php-5.2.13/ext/caleng_module
#>/Usr/localhost/webserver/php/bin/phpize
#>./Configure -- with-php-config =/usr/localhost/webserver/php/bin/php-config
#> Make
#> Make install
Now the caleng_module.so file is generated in the/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613 Directory
Add extension = caleng_module.so to the php. ini configuration file.
Work done
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.