search
HomeBackend DevelopmentPHP Tutorial[php extension and embedded] -Installing the build environment_PHP tutorial

[php extension and embedded] -Installing the build environment_PHP tutorial

Jul 14, 2016 am 10:08 AM
phpandinstallInstallEmbeddedExpandConstructenvironmentNowPretending

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); 
}

This short but complete message tells you ZendMM It cleans up the memory after you dirty it and gives you where the leaked memory block was allocated. Using this information, it is easy to locate the problem, open the file, find the corresponding line, and place it in place before the end of the function Add efree(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)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.