Develop PHP extension modules using C or C ++

Source: Internet
Author: User
If you want to use C ++ for development, you only need to extern "C" {} to include its c header file and library definition. c ++ can directly use the C library,

----------------------------------------------------

Get a PHP source code, I am using a php-5.3.0.tar.gz
Extract
Go to the ext directory.
$./Ext_skel -- extname = jason_php_module // jason_php_module is the custom Module name.
Go to the generated directory jason_php_module, which contains some files and directories.

Config. M4 // compile the configuration file in Linux
Config. w32 // compile the configuration file under Win32
Credits
Experimental
Jason_php_module.c
Jason_php_module.php
Php_jason_php_module.h
Tests

You can modify the config. M4 file as prompted. Here are several important macro commands:

* DNL is a comment;
* Php_arg_with or php_arg_enable specifies the working mode of the PHP extension module. The former means no third-party libraries are required, and the latter is the opposite;
* Php_require_cxx is used to specify that the extension uses C ++;
* Php_add_include specifies the header file directory used by the PHP extension module;
* Php_check_library specifies the PHP extension module php_add_library_with_path definition and library connection error information;
* Php_add_library (stdc ++, "", extern_name_libadd) is used to link the Standard C ++ Library to the extension
* Php_subst (extern_name_shared_libadd) indicates that the extension is compiled into a dynamic link library;
* Php_new_extension is used to specify which source files should be compiled and separated by spaces;

The default module framework generated by ext_skel is for C,
If we want to use C ++ for PHP extension, in addition to the preceding php_require_cxx and php_add_library macros, we also need to change jason_php_module.c to jason_php_module.cpp.
You must note that the makefile syntax can be used in config. M4. The snippets are as follows:

Php_require_cxx ()
Using DES = "$ Using DES 'mysql _ config -- cflags '"
Php_add_library (stdc ++, "", extra_ldflags)
Extra_ldflags = "$ extra_ldflags 'mysql _ config -- libs'-lmemcached"
Ac_check_headers ([MySQL/MySQL. H])
Cppfile = "ext_name.cpp antiforbitword. cpp antibasedict. cpp trie. cpp logger. cpp antiencodeconverter. cpp strnormalize. cpp"
Php_new_extension (ext_name, $ cppfile, $ ext_shared)


----------------------------------------------------
Modify the php_jason_php_module.h File
Because the functions and classes in the tsrm. h file are written in pure C language, use extern as follows:

Extern "C "{
# Ifdef ZTS
# Include "tsrm. H"
# Endif
}

If the php_ext_name.h header file or ext_name.cpp file uses some containers or functions in the C ++ language, you must include the header file of the corresponding C ++ library, otherwise, the corresponding C ++ function cannot be found.

----------------------------------------------------
Modify the CPP file jason_php_module.cpp.

Since config. h. php. h, php_ini.h, and ext/standard/info. h contains functions and classes such as tsrm. H is the same, it is written in pure C language, so you also need to use extern to describe the following:

Extern "C "{
# Ifdef have_config_h
# Include "config. H"
# Endif

# Include "php. H"
# Include "php_ini.h"
# Include "ext/standard/info. H"
}

The # include "php_ext_name.h" statement does not need to be included in extern "C". In addition, the macro command zend_get_module also needs to be specifically stated as follows:

# Ifdef compile_dl_ext_name
Begin_extern_c ()
Zend_get_module (ext_name)
End_extern_c ()
# Endif

In short, some C-written libraries or boom-compatible methods are used to solve the problem.

---------------------------------------------------
Now you can compile the extension module. If you want to compile it into a static module (compile it as a part of PHP into PHP), go to the PHP root directory $ php_home, delete the configure file and run buildconf ).

Then run configure with your usual parameters and add the -- enable-php5cpp item. Run make clean, make, make install, and complete other necessary operations, such as re-compiling Apache.

If you want to compile the extension module using the dynamic link library, go to the directory of your module and run the phpize command (assuming you have installed pear ), it will create a configure script for your module. Then run configure, make, and make install. If you want your module to automatically load, You need to modify PHP. ini to load the correct file. For example, add a similar line: Extension = php5cpp. So.

$ Phpize
$./Configure
$ Make

Note:

If an error occurs after running the phpize command

Processing ing:
Php api version: 20041225
Zend module api no: 20060613
Zend extension api no: 220060519
Cannot find Autoconf. Please check your Autoconf installation and
$ Php_autoconf environment variable. Then, rerun this script.

The solution is as follows:

# Fetch http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz

# Tar-zvxf m4-1.4.9.tar.gz # cd m4-1.4.9/

#./Configure & make install # CD ../

# Fetch http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz

# Tar-zvxf autoconf-2.62.tar.gz # cd autoconf-2.62/

#./Configure & make install

In this way, you can

After you use phpize to generate the configure execution file, you can use./configure -- help to view the help information and modify the config. M4 file to modify the help information of configure. Every time you modify the config. M4 file, you need to use the command phpize -- clean to clear the temporary file to remove configure.

Now your PHP extension module has been compiled. Try to run the php5cpp. php automatically generated in the Module Directory to see if everything is normal? :)

------------------------------------
I will not elaborate on how to use the extended content.
During debugging, DL ('jason _ php_module.so ') can be used in PHP ');
The application can be compiled into PHP or configured into php as needed.

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.