Install the build environment
Now you probably have at least one installed php, and have used it for web-based application development. You may have started with php .Net downloads the win32 build and runs it on IIS or the Windows version of Apache, or installs a third-party created binary using your *nix (Linux, BSD, or other POSIX-compliant distribution) distribution's package management system.
Build php
Unless you download the source code package and compile it yourself, you will definitely miss some knowledge points.
*nix tools
C developer tools The first essential tool in the collection 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 if it is by executing gcc version or cc version The compiler is installed. If it is already installed, it will respond to the installed compiler version information.
If you have not installed the compiler, you can download and install gcc in the manner officially specified by the distribution you are using. Usually this means downloading a .rpm or .deb file and executing a command to install it. Depending on the distribution you are using, you can simply try the following command: 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 used with the distribution you are using. Use the latest version of the package management system to install it, just like when installing gcc, or download the source code 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. Using newer versions of these software may work well, but these versions are proven by long-term use.
If you plan to use CVS to check out the latest PHP development version code, bison and flex are also required To construct a language interpreter. Like other packages, 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 can also You need to install the cvs client. Likewise, 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
The translator is not familiar with the windows environment, so I skipped it.
Get the php source code
When downloading php, you have a centralized choice. First, if your For distribution support, you can use apt-get source php5 to download. The advantage of this method is that the distribution you are using may have some problems that require modification of the php source code. Download from here to be sure of these problems. Has been patched to make your build less buggy. The disadvantage is that most distributions are delayed by several weeks from the official PHP release.
Another option is the preferences, at www.php.net Download php-x.y.z.tar.gz (x.y.z 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 it from snaps.php.net Download the snapshot package. On this site, the latest version of all source code in the PHP repository is packaged every few hours. Certain commits by the PHP core developers may cause it to be temporarily unavailable, but if you need it before the official release The latest features of PHP 6.0, this is the easiest place to get them.
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 , there is no obvious benefit compared to using the official release package and taking a snapshot. But if you plan to publish your extension or other application to the CVS library, it is still useful to be familiar with the checkout process.
Annotation: PHP currently uses Git to manage the code base. 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 this Introduction to the project homepage.
Configuring PHP for development
In the first chapter we discussed, whether you plan to develop extensions or dive into other applications of PHP, before building developer-friendly PHP There are two special ./configure switches you need to use, these two switches should be used together with the other switches you use when building php. Enable debugging on certain key functions of the source tree. First, it enables memory leak reporting after each request.
Recalling Chapter 3 "Memory Management", ZendMM will implicitly release the allocation for each request , but the memory is not freed before the end of the script. By running a series of regression test cases on the newly developed code, the leak points can be easily exposed, so that they can be patched between releases. Let's take a look The following code snippet:
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 is completed .When enable-debug is turned on, an error message to locate the problem will be provided to the developer:void show_value(int n) { char *message = emalloc(1024); sprintf(message, "The value of n is %d\n", n); php_printf("%s", message); }
/cvs/php5/ext/sample/sample.c(33) : Freeing 0x084504B8 (1024 bytes), script=- === Total 1 memory leaks detected ===
当然, 内存泄露并不是你会碰到的唯一难以追查的问题. 有时候, 问题是潜在的, 很少显现. 比如你通宵达旦的工作, 修改了很多的代码和源文件, 当所有事情做完后, 你自信的执行了make, 测试了一个简单的脚本, 接着就看到了下面的输出:
$ sapi/cli/php -r 'myext_samplefunc();'
Segmentation Fault
这只是表象, 那问题出在哪里呢? 查看你的myext_samplefunc()实现, 并没有显示出什么明显的线索, 使用gdb运行仅显示出一串未知的符号.
同样, enable-debug会帮到你. 通过在./configure时增加这个开关, 结果的php二进制将包含所有gdb以及其他core文件检查程序所需的调试符号, 这样可以显示出问题出在哪里.
使用这个选项重新构建, 通过gdb触发崩溃, 你现在可以看到下面的输出:
1
2
3
#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)!