Ubuntu12.04 uses C to write PHP extension module tutorial sharing_PHP tutorial

WBOY
Release: 2016-07-13 17:18:37
Original
932 people have browsed it

The system is ubuntu 12.04, apache and php have been installed, and the version of php is 5.3.10.

If you encounter permission issues during the following operations, it is recommended to switch to root operations.

1. Download source code

Check out the source code of php 5.3.10

Copy the code The code is as follows:

$ svn checkout https:/ /svn.php.net/repository/php/php-src/branches/PHP_5_3_10/

Tips: If you don’t have this command, you need to install svn first. Just use apt-get to install it directly under ubuntu:

Copy code The code is as follows:

$ sudo apt-get install subversion

Regarding the php source code, if you need to know more information, you can check the description of svn in the php wiki: https://wiki.php.net/vcs/svnfaq

2. Create module

a. After the source code is checked out, enter the source code ext directory, first use ext_skel to create a module, the module name is my:

Copy code The code is as follows:

$ ./ext_skel --extname=my

b. Enter my module:

Copy code The code is as follows:

$ cd my

c. Modify the config.m4 file and find the following content:

Copy code The code is as follows:

dnl PHP_ARG_WITH(my, for my support,
dnl Make sure that the comment is aligned:
dnl [ --with-my                                                                                                                                                                                                                                                                         is aligned:
Remove the previous dnl, and the final result is as follows:

Copy code

The code is as follows:PHP_ARG_WITH(my, for my support,
Make sure that the comment is aligned ;
Then save the file.

Tips: If you need to test whether the modification is correct, you can use php my.php to test:

Copy code

The code is as follows:

$ php my.phpconfirm_my_compiled Congratulations! You have successfully modified ext/my/config.m4. Module my is now compiled into PHP.

The above confirm_my_compiled is the default function of the module. After loading the module later, you can call this function.
3. Compile module


Or execute the following command in the directory of my module:

Copy code

The code is as follows:

$ phpize$ ./configure$ make$ make install

After the compilation is successful, the following message will be prompted:



Copy the code
The code is as follows:


Installing shared extensions: / usr/lib/php5/20090626+lfs/ indicates that the module has been compiled and copied to the php module directory.
Tips: If the phpize command is not found, you need to install php5-dev. Just use apt-get to install it directly under ubuntu.

Copy the code

The code is as follows:

$ apt-get install php5-dev 4. Load module
Edit file:

Copy code

The code is as follows:


$ vim /etc/php5/conf.d/my .ini Add the following content to load the my.so module and then save it.


Copy code

The code is as follows:

extension=my.so Restart apache


Copy code

The code is as follows:

$ /etc/init.d/apache2 restart

In a web environment, you can use phpinfo to see if the my module is loaded. The terminal can be viewed using php -i | less.

5. Write test program

If the above is executed smoothly, then the my module will be loaded, and the default method confirm_my_compiled of the my module can be called. Write the php test file:

Copy the code The code is as follows:

echo confirm_my_compiled("Hello");

Execute the test file. If you see the output below, it is successful.

Copy code The code is as follows:

Congratulations! You have successfully modified ext/my/config.m4. Module Hello is now compiled into PHP.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621656.htmlTechArticleThe system is ubuntu 12.04, apache and php have been installed, and the version of php is 5.3.10. If you encounter permission issues during the following operations, it is recommended to switch to root operations. 1. Download the source code and check out...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!