How to compile an independent PHP extension)

Source: Internet
Author: User
How to compile an independent PHP extension this article translated from the README.SELF-CONTAINED-EXTENSIONS in the PHP source code. The content marked as note is added by yourself. The content is a little old and cool, and I didn't talk about any in-depth content, but it can be used as a reference for getting started learning.

Independent PHP extensions can be distributed independently of the PHP source code. To create such an extension, you need to prepare two things:

  • Configuration file (config. m4)

  • Source code of your module

Next we will describe how to create and combine these files.

Prepare system tools

To compile and run the extension successfully on the system, you need to prepare the following tools:

  • GNU autoconf

  • GNU automake

  • GNU libtool

  • GNU m4

All of the above can be obtained from the ftp://ftp.gnu.org/pub/gnu.

Note: These tools are only available in Unix-like environments.

Modify an existing extension

To show that it is easy to create an independent extension, we should first change an extension embedded in PHP to an independent extension. Install PHP and execute the following command:

$ mkdir /tmp/newext$ cd /tmp/newext

Now you have an empty directory. Copy the files in the mysql extension directory:

$ Cp-rp php-4.0.X/ext/mysql/*. # Note: It seems that this README really needs to be updated # PHP7 has removed the mysql extension section

Now the extension is complete. execute:

$ phpize

Now you can store the files in this directory to any place independently. this extension can exist completely independently.

You need to use the following command during compilation:

$ ./configure \       [--with-php-config=/path/to/php-config] \       [--with-mysql=MYSQL-DIR]$ make install

In this way, the MySQL module can use the embedded MySQL client library or installed MySQL in the MySQL directory.

Define a new extension

We name the example extension "foobar ".

The new extension contains two resource files: foo. c and bar. c (there are some header files, but these are not just important ).

The example extension does not reference any external libraries (this is important because you do not need to specify some compilation options ).

The LTLIBRARY_SOURCES option is used to specify the name of the resource file. you can have any number of resource files.

Note: the configuration options in the Makefile. in file are described above. for details, refer to xdebug.

Modify the configuration file with the m4 suffix

The m4 configuration file can specify some additional checks. For an independent extension, you only need to make some macro calls.

PHP_ARG_ENABLE(foobar,whether to enable foobar,[  --enable-foobar            Enable foobar])if test "$PHP_FOOBAR" != "no"; then  PHP_NEW_EXTENSION(foobar, foo.c bar.c, $ext_shared)fi

PHP_ARG_ENABLE automatically sets the correct variables to ensure that the extension can be started in shared mode by PHP_NEW_EXTENSION.

The first parameter of PHP_NEW_EXTENSION is the extension name, and the second parameter is the resource file. The third parameter $ ext_shared is set by PHP_ARG_ENABLE/WITH as PHP_NEW_EXTENSION.

Always use PHP_ARG_ENABLE or PHP_ARG_WITH. Even if you are not planning to release your PHP module, these settings can ensure that the interfaces of your module and the main PHP module are integrated.

Note: PHP_ARG_ENABLE and PHP_ARG_WITH should be used to define whether the module is dynamically extended or statically compiled into PHP, just like -- enabl-xxx and -- with-xxx used for compiling PHP.

Create a resource file

Ext_skel can create common code for your PHP module. you can also write basic function definitions and C code to process function parameters. For more information, see READNE. EXT_SKEL.

Don't worry about the absence of examples. There are many modules in PHP for your reference. select a simple point and add your own code.

Note: ext_skel can generate the resource files and configuration files required by the basic module without creating them by yourself.

Modify a custom module

Put the config. m4 file and resource file in the same directory, and then execute phpize (phpize is installed when PHP 4.0 and later versions are compiled ).

If your phpize is not in the system environment variable, you need to specify the absolute path, for example:

$ /php/bin/phpize

This command automatically copies the required build files to the current directory and creates a configuration file based on config. m4.

Through the above steps, you have an independent extension.

Install extension

The extension can be compiled and installed using the following command:

$ ./configure \            [--with-php-config=/path/to/php-config]$ make install
Add sharing support to the module

Sometimes independent extensions need to be shared and loaded by other modules. Next, I will explain how to add shared support to the created foo module.

  • In config. in the m4 file, use PHP_ARG_WITH/PHP_ARG_ENABLE to set the extension so that -- with-foo = shared [,...] can be used automatically. or -- enable-foo = shared [,...] this command is used as the compilation parameter.

  • In the config. m4 file, use PHP_NEW_EXTENSION (foo,..., $ ext_shared) to build the extension.

  • Add the following code to your C language resource file:

    #ifdef COMPILE_DL_FOOZEND_GET_MODULE(foo)#endif
  • This section has been mentioned above, but I just want to emphasize it again.

    PECL website conventions

    If you want to publish your website to PECL, consider the following:

  • Add LICENSE or COPYING to package. xml

  • You need to define the version information in the extension header file. this macro will be called by foo_module_entry to declare the extension version:

    #define PHP_FOO_VERSION "1.2.3"
  • 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.