C Language extension for PHP in Linux

Source: Internet
Author: User

1. Build a php Environment
Download php 5.2.6 source code and decompress it
Compile and install the php Environment

2. Create an Extension Project

Go to the source code directory
Cd php5.2.6/ext/
./Ext_skel -- extname = my_ext
Create a project named my_ext and generate my_ext.so.
3. Change configurations and programs
$ Vi ext/my_ext/config. m4
Choose
Dnl PHP_ARG_WITH (my_ext, for my_ext support,
Dnl Make sure that the comment is aligned:
Dnl [-- with-my_ext Include my_ext support])
Modify
PHP_ARG_WITH (my_ext, for my_ext support,
Make sure that the comment is aligned:
[-- With-my_ext Include my_ext support])
Or set
Dnl PHP_ARG_ENABLE (my_ext, whether to enable my_ext support,
Dnl Make sure that the comment is aligned:
Dnl [-enable-my_ext Enable my_ext support])
Modify
PHP_ARG_ENABLE (my_ext, whether to enable my_ext support,
Make sure that the comment is aligned:
[-Enable-my_ext Enable my_ext support])
$ Vi ext/my_ext/php_my_ext.h
Set
PHP_FUNCTION (confirm_my_ext_compiled);/* For testing, remove later .*/
Change
PHP_FUNCTION (say_hello );

$ Vi ext/my_ext/my_ext.c
Set
Zend_function_entry php5cpp_functions [] = {
PHP_FE (confirm_my_ext_compiled, NULL)/* For testing, remove later .*/
{NULL, NULL, NULL}/* Must be the last line in php5cpp_functions [] */
};
Change
Zend_function_entry php5cpp_functions [] = {
PHP_FE (say_hello, NULL)
{NULL, NULL, NULL}/* Must be the last line in php5cpp_functions [] */
};
Add at the end:
PHP_FUNCTION (say_hello)
{
Zend_printf ("hello world \ n ");
}
4. Compile
$ Cd my_ext
$/Usr/local/php/bin/phpize
Ps: If: Cannot find autoconf ....... To install autoconf)
$./Configure -- with-php-config =/usr/local/php/bin/php-config
$ Make
At this time, my_ext/modules/my_ext.so will be compiled
5. Configure php. ini
Put my_ext.so in the/usr/local/php/ext/directory.
$ Vi php. ini

Add the following changes:
Extension_dir = '/usr/local/php/ext /'
Extension = my_ext.so
Vi. Test
$ Vi test. php
<? Php
Say_hello ();
?>
$/Usr/local/php/bin/php test. php
Hello world.
The success is achieved.

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.