Home  >  Article  >  Backend Development  >  Generation and compilation of PHP extensions

Generation and compilation of PHP extensions

高洛峰
高洛峰Original
2016-11-17 14:20:111472browse

First of all, let me explain that there are two compilation methods for PHP extensions:
Method 1: Compile the extension directly when compiling PHP
Method 2: The extension is compiled into a .so file, and the loading path is configured in php.ini;

The following Let’s start with the steps to create and compile a PHP extension:
Download the PHP source code, unzip it, and start the operation in the root directory of the source code,
1. Use ext_skel to generate the extension framework, as follows:

➜ php-5.6.24 cd ~/Downloads /tmp/php-5.6.24➜ php-5.6.24 cd ext
➜ ext ./ext_skel --extname=myfirstext

After ext_skel is executed, the developer will be prompted for subsequent steps. This step is The steps for method one of the two compilation methods of the extension are as follows:

To use your new extension, you will have to execute the following steps:

 $ cd ..
 $ vi ext/plogger/config.m4
 $ ./buildconf
 $ ./configure --[with|enable]-plogger
 $ make
 $ ./sapi/cli/php -f ext/plogger/plogger.php
 $ vi ext/plogger/plogger.c
 $ make

2. Modify the file ext/myfirstext/config.m4
Focus on the code of line10-18, which is used to enable the command of this extension when setting ./configure option, delete the dnl of line16 and line18, and understand the dnl as a comment character.

dnl Otherwise use enable:
16 dnl PHP_ARG_ENABLE(myfirstext, whether to enable myfirstext support,
dnl Make sure that the comment is aligned:
dnl [ --enable-myfirstext Enable myfirstext support])
20 if test "$PHP_MYFIRSTEXT" != "no"; then
dnl Write more examples of tests here...

The above two steps are public. The following will introduce two ways to compile PHP extensions.
Method 1: Compile the extension directly when compiling PHP
3. Execute ./buildconf in the source code root directory, as follows
4 . Execute ./configure –enable-myfirstext in the source code root directory. In order to reduce compilation time, you can specify not to compile certain modules during the configure phase, such as:

./configure --without-iconv --enable-debug -- enable-myfirstext --disable-cgi --enable-cli --without-pear --disable-xml --without-mysql


5. Execute make in the source code root directory

Note that after successful compilation, do not execute make install Because at this point, the extension myfirstext has been compiled successfully and the corresponding php binary file has been generated. It is in ./sapi/cli/php

Method 2: The extension is compiled into a .so file and configured in php.ini Loading path

3. Execute the phpize command under the extension directory ext/myfirstext/
4. Execute the ./configure –enable-myfirstext command under the extension directory ext/myfirstext/
5. Execute make
under the extension directory ext/myfirstext/ After executing make, the corresponding .so file will be generated under ext/myfirstext/modules. Just configure and load this file in php.ini.

Verify whether the extension is loaded successfully

Execute ./sapi/cli/php -f ext/myfirstext/myfirstext.php
Or list all extensions through php -m to see if there is myfirstext, execute the command: ./sapi/cli /php -m | grep myfirstext
Passing the above verification indicates that the extension was compiled successfully. But so far, I have not edited the C-related code. Everything is generated by ext_skel by default. Check what functions this extension myfirstext contains? As follows:

➜ php-5.6.24 ./sapi/cli/php -r 'print_r(get_extension_funcs("myfirstext"));'


OK, so far you are familiar with the generation, configuration, and compilation of the PHP extension framework . Next, we need to add a function of our own to the extension myfirstext.


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