[PHP extensions and embedded]-Installation build Environment _php Tutorial

Source: Internet
Author: User
Tags zts using git automake
Install the Build environment


Now you may have at least one installed PHP and already use it for Web-based application development. You may have been from PHP. NET downloaded Win32 build and run on IIS or Windows version of Apache, or use the package management system of your *nix (Linux, BSD, or other POSIX release) release to install the binary created by a third party.

Build PHP

Unless you are downloading the source package yourself compile, otherwise you will certainly miss some knowledge points.

*nix Tools

The first tool necessary for the C developer Toolset is the C compiler. Your release may contain one by default, and if you're lucky, it's GCC (the GNU Compiler Collection). You can check whether the compiler is installed by executing GCC version or CC version, and it responds to the installed compiler version information if it is already installed.

If you do not have a compiler installed, you can download and install GCC in the same manner as the official version of the distribution you are using. Typically this means downloading a. rpm or. deb file and executing a command to install it. Depending on the release version you use, you can simply use the following command to try the installation: Urpmi gcc, apt-get install gcc, pkg-add-r gcc, emerge gcc.

In addition to the compiler, you need the following programs and tools: Make, autoconf, Automake, Libtool. These tools can also be installed with the package management system of the distribution you are using, just as you would when installing GCC, or by downloading the source package from gnu.org itself.

The recommended versions are: Libtool 1.4.3, autoconf 2.13, Automake 1.4 or 1.5. Newer versions of these software may also work well, but these versions are validated for long-term use.

If you plan to use CVS to check out the latest PHP development code, you'll also need bison and flex to construct the language interpreter. As with other packages, these two packages can be installed using your Release package management system or compiled from gnu.org download source.

If you choose CVS, you will also need to install the CVS client. Again, it may already be installed on your distribution, or you can download the compilation yourself. Unlike the other packages is this package you need to download in cvshome.org.

WIN32 Tools

The translator is unfamiliar with the Windows environment and is therefore skipped.

Get PHP Source code

When you download PHP, you have the option to focus. First, if your distribution is supported, you can use the Apt-get source php5 command to download. A bit of this way is that you use the distribution may have some problems need to modify the PHP source code, download from here, you can be sure that these issues have been patched so that your build there are fewer problems. The downside is that most distributions are delayed for weeks than the official PHP release.

Another option is the preference, www.php.net download php-x.y.z.tar.gz (x.y.z is the current release). These PHP releases have been tested by countless PHP users around the world and are up to date.

You can also download the snapshot package from Snaps.php.net. On this site, the latest version of all source code in the PHP repository will be packaged once every few hours. Some submissions from PHP kernel developers may cause it to be temporarily unavailable, but if you need the latest PHP 6.0 features before the official release, This is the easiest place to get.

Finally, you can use CVS to get directly to the development version used by the PHP kernel development team. If you're just developing extensions and embedded programs, there's no obvious benefit to using the official release package and getting snapshots. But if you plan to publish your extensions or other applications to the CVS repository, it's useful to be familiar with the checkout process.

PHP is now using git to manage the code base, about the CVS check out no longer repeat, please visit https://github.com/php/php-src to get the latest source code. If you want to contribute code to PHP, you can see the introduction of the project's homepage.

Configure the PHP for development

In the first chapter we discuss whether you plan to develop extensions or sneak into other PHP applications, and there are two special./configure switches that you need to use when building developer-friendly PHP. The two switches should be used with other switches you use when building PHP.

Enable-debug

Start debugging on some key functions in PHP and Zend source tree. First it enables a memory leak report after each request ends.

Recalling chapter III, "Memory Management", ZENDMM implicitly releases memory allocated for each request, but not released until the end of the script. By running a series of regression test cases on newly developed code, the leak points can be easily exposed so that they can be patched between releases. Let's take a look at the following code snippet:

void Show_value (int n) {     char *message = Emalloc (1024x768);        sprintf (Message, "The value of N is%d\n", n);     php_printf ("%s", message); }

If this stupid piece of code is executed during PHP request execution, it will leak 1024 bytes of memory. In general, ZENDMM releases it after the execution of the script is complete.

When Enable-debug is turned on, it will provide the developer with an error message for locating the problem:

/CVS/PHP5/EXT/SAMPLE/SAMPLE.C:  freeing 0x084504b8 (1024x768 bytes), script=-= = Total 1 Memory leaks detected = = =

This short but complete message tells you that the ZENDMM has been cleaned up after you have soiled the memory and given where the leaked memory blocks are allocated. With this information, it is easy to locate the problem, open the file, find the corresponding line, and add efree (message) at the appropriate position before the function ends.

Of course, memory leaks are not the only hard-to-track problem you'll encounter. Sometimes, the problem is latent and seldom appears. For example, you work all night long, modify a lot of code and source files, when everything is done, you confidently executed make, tested a simple script, and then saw the following output:

$ Sapi/cli/php-r ' myext_samplefunc (); '

Segmentation Fault

It's just an appearance, what's the problem? Looking at your Myext_samplefunc () implementation, and not showing any obvious clues, using GDB to run only shows a string of unknown symbols.

Likewise, Enable-debug will help you. By adding this switch at./configure, the resulting PHP binaries will contain all of the debug symbols required by GDB and other core File Checker to show where the problem is.

Using this option to rebuild and trigger the crash via GDB, you can now see the following output:

1

2

3

#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 null-terminated, and the garbage behind can prove it, not the binary-safe function that uses it. The STRCHR () implementation attempts to scan the incoming STR from beginning to end, but because there is no terminating null byte, it reaches the memory that does not belong to it, which results in a segment error. We can use the MEMCHR () and strlen parameters to fix this problem to prevent crashes.

Enable-mantainer-zts

This option forces PHP build to enable thread safety resource management (TSRM)/zend thread Safety (ZTS) layer. This switch increases the complexity of processing, but for developers, you will find this a good thing. For a detailed introduction to ZTS and why to turn this option on at development time, refer to Chapter I.

Enable-embed

If you want to develop a different application that embeds PHP, you need another very important switch. This switch opens and builds a mod_php5.so dynamic link library that is similar to the one that opened With-apxs: libphp5.so, which can be used to embed PHP in other applications.

Compiling on Unix

Now that you have all the tools you need, download the PHP source package and get to know all the required./configure switches, it's time to actually compile PHP.

This assumes that you downloaded the php-5.1.0.tar.gz, placed in your home directory, you will use the following command sequence to unpack the source package, and switch to the extracted source 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 modified slightly:

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

Now, with the required switches and other options you want to turn on or off, execute the./configure command:

[/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, the output of a lot of information on your screen is finally completed./configure stage. Then you can start compiling:

[/home/sarag]$ Make all Install

Now, stand up and have a cup of coffee. Compilation time can take a few minutes on high-performance machines and may take up to half an hour on the old 486. Once the build process is complete, you have a properly configured, fully functional PHP that can be used for development.

Compiling on the Win32

The translator is unfamiliar with the Windows environment and is therefore skipped.

Summary

Now that PHP is installed with the correct options, you are ready to develop a real, functional extension. Later chapters, you begin to dissect the PHP extension. Even if you only plan to embed PHP into your app without any extensions to the language, you should also read these chapters because it explains in detail how PHP works.

The above is [translation][php extended development and Embedded] Chapter 4th-Install the content of the building environment, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.