Home > Article > Backend Development > Generation and compilation of PHP extensions
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:
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
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.
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: