Create C ++ extension for PhP4 on Linux

Source: Internet
Author: User

Jason
1. required tools
2. Main Steps

2.1. Create
2.2. Compile
2.3. debugging

3. Summary

1. required tools

The following tools are required:

1.

Gcc. I am using 3.4.3
2.

Autoconf. I am using 2.13-5. It is strange that 2.59 is not usable. If you have installed this version in your system, uninstall it first and then install 2.13.
3.

PhP4Source code. I am using a PHP-4.4.4. You can download

2. Main Steps

Creating PHP extensions is troublesome. People who are familiar with other scripting languages will feel that the development of PHP extensions has reached the extreme. However, the principle of writing extensions to scripts is the same:

Export a function with a specific name, and then return all information about the extension through this function. The important ones are:

1.

Name, description, and version information
2.

Functions provided by the module, which are generally an array of function names and function pointers.
3.

Use some functions to exchange data between scripts and extensions. in this regard, various languages are different. TCL directly transmits strings. Python provides functions such as pyarg_parsetuple and py_buildvalue, and Lua exchanges data through a stack.

PHP provides a series of tools and macros to encapsulate this process. Unfortunately, in my opinion, this makes it more complex.
2.1. Create

1.

Place the source of the PHP-4CodeDecompress. This directory is called phpsrc.
2.

Go to the phpsrc/EXT directory and run the./ext_skel extension. Suppose our extension name is testext, Which is input./ext_skeltestext.ProgramThe following content will be created for you in the ext directory:
1.

Testext directory
2.

The testext directory also includes:
1.

Testext. C-this is the source file of the extension module.
2.

Php_testext.h-header file
3.

Config. M4-the configuration file used for Autoconf. Autoconf decides how to compile and link this module through the content of this file.
4.

Testext. php-automatically generated test script
5.

Tests directory-ignore
3.

Then we must briefly understand the config. M4 file.

M4 is a macro processor, in the sense that it copiesits input to the output, expanding macros as it goes. macros are either builtinor user-defined, and can take any number of arguments. besides just doing macroexpansion, M4 has builtin functions for including
named files, running shellcommands, doing integer arithmetic, manipulating text in various ways, specify Ming recursion, Etc .... m4 can be used either as a front-end to acompiler, or as a macro processor in its own right.

the M4 macro processor is widely available on allunixes, and has been standardized by POSIX. usually, only a small percentage ofusers are aware of its existence. however, those who find it often becomecommitted users. the popularity of GNU Autoconf, which
requires GNU M4 forgenerating configure scripts, is an incentive for running to install it, whilethese people will not themselves program in M4. GNU M4 is mostly compatiblewith the System V, Release 3 version, doesn t for some minor differences. seecompatibility,
for more details.

Some people find M4 to be fairly addictive. theyfirst use M4 for simple problems, then take bigger and bigger challenges, learning how to write complex sets of M4 macros along the way. once reallyaddicted, users pursue writing of sophisticated M4 applications
Even to solvesimple problems, faster ting more time debugging their M4 scripts than doing realwork. Beware that M4 may be dangerous for the health of compulsive programmers.

The above content comes from http://www.gnu.org, so I won't talk much about it. In short, PHP uses this technology to help us build extensions.

Record only what is useful to us:
1.

DNL is a comment.
2.

Php_arg_with or php_arg_enable specifies the working mode of the PHP module. Select either of them.
3.

Php_require_cxx ()Used to specify the extension to use C ++
4.

Php_subst (sysfile_shared_libadd)It indicates that this extension is compiled into the form of a dynamic link library (sysfile = modulename, this sentence does not work if it is omitted. During the test, you can know that this sentence adds a line to the makefile: sysfile_shared_libadd =-l stdc ++. Although this sentence is not compiled, it is loaded in PHP **. so file, the extension function cannot be found)
5.

Php_add_library (stdc ++, "", sysfile_shared_libadd)Used to link the Standard C ++ Library to the extension
6.

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. We need to use C ++. The above three macros, 3, 4, and 5, are required. in addition, testext. C is renamed to testext. CPP, so

The testext. c file originally included in php_new_extension also needs to be modified.

After using C ++, pay attention to a small problem, that is, the php_testext.h file may be referenced by other parts of PHP, And the quotor may be. c file, so it cannot contain anything unique to C ++ in php_testext.h. for example, the standard template library, class, or bool type.
4.

Default testext. C (now renamed testext. CPP). It already contains a test-type export function. You can use that example to understand how to add your own function. zend_function_entry is the list of exported functions. zend_module_entry describes the module information. but because it is C ++, there are several points to modify:
1.

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

Use extern "c" to modify.
2.

Zend_get_module must also be modified with extern "C"
3.

Zend_module_entry is the type used to describe the module information. It is actually a structure, and the second item does not know what it means. in Windows, it seems to be a description, which can be a long string, but in Linux it seems that it can only be the same as the module name.
5.

The subsequent work is to write your own code. According to the design, provide external interface functions, and then write C/C ++ code for implementation.

2.2. Compile

1.

After the code is written, go to the phpsrc directory and run. /buildconf -- force: unless the M4 file is wrong or the Autoconf installation is faulty, there will be no errors in this step and the time will not be long. you can force buildconf to regenerate the configure file.
2.

Run./configure -- disable-all -- With-testext = shared

-- Disable-all is used to reduce the time for configuration and next compilation, because we only need our own modules.

-- With-testext = shared indicates that this module is compiled into a dynamic link library instead of integrated into PHP.
3.

Run make

This process takes a long time. If your code has a problem, a compilation or link error may occur. You can modify the code as prompted. if there is no problem, the extension file testext will be generated under phpsrc/modules. also, phpsrc/SAPI/CLI/PHP is generated. can run. /SAPI/CLI/appext/testext. PHP to test whether the module is normal. however, testext. so is copied to a specific directory. Different systems in this directory are different. You can run/SAPI/CLI/appext/testext. PHP error message to know this directory.
4.

If there is no problem with the test, you can install it in the official PHP. Modify the php. ini file and restart Apache.

2.3. debugging

You can use GDB to debug PHP extensions. first, write a PHP script to test the function you want to test. Put it in the phpsrc directory, for example, test. PHP. perform the following steps:

1.

GDB
2.

File./SAPI/CLI/PHP
3.

Run the B command to run the breakpoint. because it is a C ++ program, the extended export function name is not our input name. You can use the NM command to view all the extended export functions and find the name finally allocated by the compiler. this name must contain the name you entered.

GDB will prompt that this symbol cannot be found in the current program (PHP). If you want to find this symbol in the shared library in the future, the answer is yes.
4.

Run test. php, and then it will stop at the breakpoint. debug with commands such as N, S, and display.

3. Summary

In a word, PHP module extensions are more troublesome than anyone else and there are too many rules. in fact, I want it to provide a document that provides the information I described at the beginning of section 2. however, it may be because I am not good at learning the art, and I am absolutely so disgusted with what is actually normal.

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.