[Php extension development and embedded] Chapter 4th-install and build the environment

Source: Internet
Author: User
Tags zts automake
Now you may have at least one installed php and have used it for web-based application development. you may already have. net Download win32 build and run on iis or windows apache, or use your * nix (Linux, bsd, or other POSIX-compliant releases) the package management system of the release version installs binary files created by a third party. install build environment

Now you may have at least one installed php and have used it for web-based application development. you may already have. net Download win32 build and run on iis or windows apache, or use your * nix (Linux, bsd, or other POSIX-compliant releases) the package management system of the release version installs binary files created by a third party.

Build php

Unless you download the source code package and compile it yourself, you will miss some knowledge points.

* Nix tool

The first essential tool in the C developer tool set is the C compiler. your release may include one by default. if lucky, it is the gcc (GNU compiler set ). you can run gcc version or cc version to check whether the compiler is installed. If yes, it will respond to the installed version information.

If you have not installed the compiler, you can download and install gcc as specified by the official release. this usually means to download. rpm or. deb file, and execute a command to install it. this depends on the release version you are using. you can use the following command to install urpmi gcc, apt-get install gcc, pkg-add-r gcc, and emerge gcc.

In addition to the compiler, you also need the following programs and tools: make, autoconf, automake, and libtool. these tools can also be installed using the package management system of the release version you are using, just like Installing gcc, or directly download the source package from gnu.org and compile it yourself.

The recommended versions are libtool 1.4.3, autoconf 2.13, automake 1.4, or 1.5. the updated versions of these software may work well, but these versions have been verified for a long time.

If you plan to use CVS to check out the latest php development code, you also need bison and flex to construct the language interpreter. like other packages, these two packages can be installed using your release package management system, or you can download the source code from gnu.org and compile it yourself.

If you select CVS, you also need to install the cvs client. similarly, it may have been installed on your release, or you can download and compile it yourself. unlike other packages, you need to download this package at cvshome.org.

Win32 tool

The translator is not familiar with the windows environment, so he skipped it.

Obtain php source code

When downloading php, you have a concentrated choice. first, if your release supports, you can use commands such as apt-get source php5 to download. this method is a bit because the release you are using may have some problems and you need to modify the php source code. download it from here, it is certain that these problems have been patched to make your build have fewer problems. the disadvantage is that most releases will be several weeks behind official php releases.

Another option is Preference. download php-x.y.z.tar.gz (x. y. z is the current version) on www.php.net. these php versions have been tested by countless php users around the world and are the latest.

You can also download the snapshot package from snaps.php.net. on this site, the latest version of all source code in the php version library will be packed every several hours. some submission by php kernel developers may temporarily make it unavailable. However, if you need the latest php 6.0 features before the official release, this is the easiest way to get it.

Finally, you can use cvs to directly obtain the development version used by the php kernel development team. if you only want to develop extensions and embedded programs, there is no obvious benefit compared to using official release packages and getting snapshots. however, if you plan to release your extension or other applications to the CVS library, it is useful to be familiar with the check-out process.

Php currently uses Git to manage the code library. for details about cvs check-out, visit https://github.com/php/php-srcto obtain the latest source code. if you want to contribute code to php, you can view the home page of this project.

Configure php for development

As discussed in Chapter 1, whether you plan to develop extensions or dive into other php applications, there are two special features when building developer-friendly php. you need to use the/configure switch. These two switches should be used together with other switches used when you build php.

Enable-debug

Enable debugging on some key functions in the php and zend source code trees. First, it enables the memory leak report after each request ends.

Looking back at Chapter 3 "memory management", ZendMM implicitly releases the memory allocated for each request, but is not released before the script ends. by running a series of regression test cases on the newly developed code, leakage points can be easily exposed, so that they can be repaired between releases. let's take a look at the following code snippet:

void show_value(int n)  {      char *message = emalloc(1024);        sprintf(message, "The value of n is %d\n", n);      php_printf("%s", message);  }

If this stupid code is executed during php request execution, it will leak 1024 bytes of memory. generally, ZendMM will release it after the script execution is complete.

When enable-debug is enabled, the following error messages are provided for developers to locate the problem:

/cvs/php5/ext/sample/sample.c(33) :  Freeing 0x084504B8 (1024 bytes), script=-  === Total 1 memory leaks detected ===

This short but complete message tells you that ZendMM cleans up the memory and shows where the leaked memory block is allocated. with this information, it is easy to locate the problem, open the file, find the corresponding row, and add efree (message) at the appropriate position before the function ends ).

Of course, memory leakage is not the only hard-to-trace problem you will encounter. sometimes, problems are potential and seldom appear. for example, you have modified a lot of code and source files by working all night long. after everything is done, you are confident to execute make and test a simple script, then we can see the following output:

$ sapi/cli/php -r 'myext_samplefunc();'  Segmentation Fault

This is just a representation. where is the problem? Check your myext_samplefunc () implementation. there is no obvious clue. running gdb only shows a string of unknown symbols.

Similarly, enable-debug will help you. in. /add this toggle when configure, and the php binary result will contain all the debugging symbols required by gdb and other core file check programs. this will show where the problem is.

Use this option to re-build and trigger the crash through gdb. now you can see the following output:

#0 0x1234567 php_myext_find_delimiter(str=0x1234567 "foo@#(FHVN)@\x98\xE0...",                                        strlen=3, tsrm_ls=0x1234567)      p = strchr(str, ',');

The goal becomes clear. the str string is not terminated by NULL. the following garbage can prove this, rather than the binary safe function uses it. strchr () is used to scan the input str from start to end. However, since the NULL byte is not terminated, it reaches the memory that does not belong to it, which leads to a segment error. we can use memchr () and strlen parameters to fix this problem and prevent crash.

Enable-mantainer-zts

This option forces php to build and enable the thread security resource management (TSRM)/Zend thread security (ZTS) layer. this switch will increase the processing complexity, but for developers, you will find this is a good thing. for more information about ZTS and how to enable this option during development, see Chapter 1.

Enable-embed

If you want to develop another application embedded in php, you need another very important switch. after this switch is turned on, a dynamic link library (mod_php5.so) similar to the dynamic link library (libphp5.so) built after with-apxs is turned on will be built. it can be used to embed php into other applications.

Compile on Unix

Now you have all the tools you need, downloaded the php source code package, and recognized all the required./configure switches. it is time to compile php.

In this case, you download php-5.1.0.tar.gz in your home directory. you will use the following command sequence to unpack the source package and switch to the decompressed Source Code Directory:

[/home/sarag]$ tar -zxf php-5.1.0.tar.gz  [/home/sarag]$ cd php-5.1.0

If you are not using gnu tar, the command may need to be slightly modified:

[/home/sarag]$ gzip -d php-5.1.0.tar.gz | tar -xf -

Now, run the./configure command with the required switch and other options you want to enable or disable:

[/home/sarag/php-5.1.0]$ ./configure enable-debug \  enable-maintainer-zts disable-cgi enable-cli \  disable-pear disable-xml disable-sqlite \  without-mysql enable-embed

After a period of processing, you have output a lot of information on your screen, and finally completed the./configure stage. then you can start compiling:

[/home/sarag]$ make all install

Now, stand up and have a cup of coffee. it may take several minutes to compile on a machine with high performance, or even half an hour on the old 486. after the build process is complete, you have a correct configuration, complete functions, and can be used to develop php.

Compile on Win32

The translator is not familiar with the windows environment, so he skipped it.

Summary

Now php has been installed with the correct options. you are ready to develop a real, functional extension. next, we will start to analyze php extensions. even if you only plan to embed php into your application, instead of making any language extensions, you should also read these chapters because they explain in detail the php operating mechanism.

The above is the [Translation] [php extension development and embedded] Chapter 4th-install and build the environment content. For more information, please follow the PHP Chinese network (www.php1.cn )!

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.