Home > Article > Backend Development > [php extension development and embedded] Chapter 4 - Installing the build environment
Install the build environment
Now you probably have at least one installed php, and have been using it for web-based application development. You may have downloaded the win32 build from php.Net and run it in Apache for iis or windows, or use your *nix (Linux, bsd, or other POSIX-compliant distribution) distribution's package management system to install a third-party created binary.
Build php
Unless you download the source code package and compile it yourself, you will definitely miss some knowledge points.
*nix tools
The first indispensable tool in the C developer toolset is a compiler for C. Your distribution may include one by default, and if you are lucky, it is gcc (GNU Compiler Collection). You can check whether the compiler is installed by executing gcc version or cc version. If it is already installed, It will respond to the installed compiler version information.
If you don't have a compiler installed, you can download and install gcc in the official way specified by the distribution you are using. Usually this means downloading one. rpm or .deb file, and execute a command to install it. This depends on the distribution version you are using, you can simply use the following commands to try to install it: urpmi gcc, apt-get install gcc, pkg-add -r gcc , emerge gcc.
In addition to the compiler, you also need the following programs and tools: make, autoconf, automake, libtool. These tools can also be installed using the package management system of the distribution you are using, and The same as when installing gcc, or download the source package directly from gnu.org and compile it yourself.
The recommended versions are: libtool 1.4.3, autoconf 2.13, automake 1.4 or 1.5. Newer versions of these software may also be used It can work very well, but these versions have been verified by long-term use.
If you plan to use CVS to check out the latest PHP development version code, you will also need bison and flex to construct the language interpreter. And other packages Likewise, these two packages can be installed using your distribution's package management system, or you can download the source code from gnu.org and compile it yourself.
If you choose CVS, you also need to install the cvs client. Likewise, it It may already be installed on your distribution, or you can download and compile it yourself. Unlike other packages, you need to download this package from cvshome.org.
Win32 Tools
译Readers who are not familiar with the Windows environment, so skip it.
Get PHP source code
When downloading PHP, you have a centralized choice. First, if your distribution supports it, you can use apt- Use a command like get source php5 to download. The advantage of this method is that the distribution version you are using may have some problems that require modification of the php source code. If you download it from here, you can be sure that these problems have been patched so that your build can exist. Fewer problems. The disadvantage is that most distributions will be delayed by several weeks than the official PHP release.
Another option is the preferences, download php-x.y.z.tar.gz(x.y.z at www.php.net is the current release version). These PHP releases have been tested by countless PHP users around the world and are the latest.
You can also download snap packages from snaps.php.net. On this site, php The latest version of all source code in the repository is packaged every few hours. Certain commits by the PHP core 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 it.
Finally, you can use cvs to directly get the development version used by the php kernel development team. If you just want to develop extensions and embedded programs, compared to using the official release package and getting Snapshots, this will not have any obvious benefits. But if you plan to publish your extension or other application to the CVS repository, it is still useful to be familiar with the checkout process.
Annotation: PHP is currently managed using Git Code library, I won’t go into details about cvs checkout. Please visit https://github.com/php/php-src to get the latest source code. If you want to contribute code to PHP, you can check the introduction on the homepage of the project.
Configuring PHP for Development
As we discussed in Chapter 1, whether you plan to develop extensions or dive into other applications of PHP, there are two special ./configure when building developer-friendly PHP. switches you need to use, these two switches should be used together with other switches you use when building php.
enable-debug
Enable on certain key functions in php and zend source trees Debugging. First it enables memory leak reporting after each request.
Recalling Chapter 3 "Memory Management", ZendMM will implicitly release the memory allocated for each request, but it is not allocated before the end of the script. Freed memory. By running a series of regression test cases on newly developed code, 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(1024); sprintf(message, "The value of n is %d\n", n); php_printf("%s", message); }
If this stupid code is executed during the execution of the php request, it will leak 1024 bytes of memory. Normally ZendMM will release it after the script execution ends.
In enable-debug When turned on, it will provide developers with an error message 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 cleaned up after you dirty the memory and gives the leaked memory Where the block is allocated. Using 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 end of the function.
当然, 内存泄露并不是你会碰到的唯一难以追查的问题. 有时候, 问题是潜在的, 很少显现. 比如你通宵达旦的工作, 修改了很多的代码和源文件, 当所有事情做完后, 你自信的执行了make, 测试了一个简单的脚本, 接着就看到了下面的输出:
$ sapi/cli/php -r 'myext_samplefunc();' Segmentation Fault
这只是表象, 那问题出在哪里呢? 查看你的myext_samplefunc()实现, 并没有显示出什么明显的线索, 使用gdb运行仅显示出一串未知的符号.
同样, enable-debug会帮到你. 通过在./configure时增加这个开关, 结果的php二进制将包含所有gdb以及其他core文件检查程序所需的调试符号, 这样可以显示出问题出在哪里.
使用这个选项重新构建, 通过gdb触发崩溃, 你现在可以看到下面的输出:
#0 0x1234567 php_myext_find_delimiter(str=0x1234567 "foo@#(FHVN)@\x98\xE0...", strlen=3, tsrm_ls=0x1234567) p = strchr(str, ',');
目标就变得清晰了. str字符串并不是NULL终止的, 后面的垃圾可以证明这一点, 而非二进制安全的函数使用了它. strchr()实现尝试从头到尾的扫描传入的str, 但由于没有终止NULL字节, 它到达了不属于它的内存, 这就导致了段错误. 我们可以使用memchr()和strlen参数来修复这个问题防止崩溃.
enable-mantainer-zts
这个选项强制php构建启用线程安全资源管理(TSRM)/Zend线程安全(ZTS)层. 这个开关会增加处理时的复杂度, 但是对于开发者而言, 你会发现这是一件好事情. 关于ZTS的详细介绍以及为什么在开发时要开启这个选项, 请参考第一章.
enable-embed
如果你要开发一个嵌入php的其他应用, 就需要另外一个非常重要的开关. 这个开关打开后就会构建出一个类似开启了with-apxs后构建出的mod_php5.so动态链接库: libphp5.so, 它可以用于将php嵌入到其他应用中.
在Unix上编译
现在你已经有了所有需要的工具, 下载了php源码包, 认识了所有需要的./configure开关, 是时候真正的编译php了.
这里假设你下载的是php-5.1.0.tar.gz, 放在了你的主目录, 你将使用下面的命令序列解包源码包, 并切换到解压出的源码目录:
[/home/sarag]$ tar -zxf php-5.1.0.tar.gz [/home/sarag]$ cd php-5.1.0
如果你使用的不是gnu的tar, 命令可能需要略作修改:
[/home/sarag]$ gzip -d php-5.1.0.tar.gz | tar -xf -
现在, 用所需的开关和其他你想要开启或禁用的选项, 执行./configure命令:
[/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
在一段时间的处理后, 在你的屏幕上输出了很多的信息, 最终完成了./configure阶段. 接下来你就可以开始编译了:
[/home/sarag]$ make all install
现在, 站起来喝杯咖啡吧. 编译的时间在性能高的机器上可能需要几分钟, 在旧的486上甚至可能需要半个小时. 构建处理完成后, 你就拥有了一个正确配置, 功能完整, 可用于开发的php.
在Win32上编译
译者不熟悉windows环境, 因此略过.
小结
现在php已经以正确的选项安装了, 你已经准备好开发一个真实的, 有功能的扩展了. 后面的章节, 就开始剖析php扩展. 即便你只计划将php嵌入到你的应用中, 而不对语言做任何扩展, 你也应该阅读这些章节, 因为它详细解释了php的运行机制.
以上就是 [翻译][php扩展开发和嵌入式]第4章-安装构建环境的内容,更多相关内容请关注PHP中文网(www.php.cn)!