# 4.1 動手編譯PHP
從一個PHP程序猿,到一個想為PHP開發擴展的程序猿,此間的進化有一步是跳不過去的,那就是你必須熟知如何編譯PHP的源碼。
### *nix Tools
C語言的編譯器是我們使用C語言的必備工具,你的系統應該已經自帶了一種C語言的編譯器,而且它極有可能是大名鼎鼎的GCC。通過檢測你本機gcc或者cc程序的版本,可以很方便的知道你機器上是否已經安裝的某種C語言的編譯器。
````shell
walu@walu-ThinkPad-Edge:~$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
````
如果你還沒有安裝編譯器,那你需要安裝一個。最簡單的辦法便是去下載一個與你系統相符的rpm或者deb包,當然你也可以通過以下命令的一種來方便的安裝:yum install gcc, apt-get install gcc, pkg-add -r gcc, 或者 emerge gcc.
除了編譯器,你還需要以下程序:make, autoconf, automake, 和libtool。說實話,我連autoconf現在是啥還不知道(截至到現在,2011年9月6號),不過除非RP太低,一般系統中都會自備了,而且phpize程序會把這些需要的腳本給生成好的。
對于編譯需要的程序以及它們的版本我們可以在PHP官網找到最新的答案:
<ul>
<li>autoconf: 2.13 (2.59+ for PHP 5.4+)</li>
<li>automake: 1.4+</li>
<li>libtool: 1.4.x+ (except 1.4.2)</li>
<li>bison: 1.28, 1.35, 1.75, 2.0 or higher</li>
<li>flex (PHP 5.2 and earlier): 2.5.4 (not higher)</li>
<li>re2c: 0.13.4+</li>
</ul>
你千萬不要被上面的清單給嚇著,其實系統應該給裝備好了,除非真RP低,那你出門去買張彩票吧... ...
當然,我們也可以通過SVN從PHP源碼庫里導出一份源碼,需要注意的是,PHP的svn源碼庫地址是https協議的。官方推薦我們直接簽出它的php-src目錄:
````shell
$ svn checkout https://svn.php.net/repository/php/php-src --depth immediates php-src
$ cd php-src
````
當然,我們也可以簽出特定的版本:
<ul>
<li>PHP 5.3: svn checkout https://svn.php.net/repository/php/php-src/branches/PHP_5_3 php-src-5.3 </li>
<li>PHP 5.4: svn checkout https://svn.php.net/repository/php/php-src/branches/PHP_5_4 php-src-5.4 </li>
<li>PHP HEAD: svn checkout https://svn.php.net/repository/php/php-src/trunk php-src-trunk </li>
</ul>
最新的大家可以來這查看:http://php.net/svn.php
### Win32 Tools
這里僅代表作者05年的觀點,我還沒有在win平臺下測試過,稍後會把這段修正過來。
<pre>
The Win32/PHP5 build system is a complete rewrite and represents a significant leap forward from the PHP4 build system. Instructions for compiling PHP4 under Windows are available on php.net, only the PHP5 build systemwhich requires Windows 2000, Windows 2003, or Windows XPwill be discussed here.
First, you'll need to grab libraries and development headers used by many of the core PHP extensions. Fortunately, many of these files are redistributed from php.net as a single .zip file located at http://www.php.net/extra/win32build.zip.
Create a new directory named C:\PHPDEV\ and unzip win32build.zip using your favorite zip management program into this location. The folder structure contained in the zip file will create a subdirectory, C:\PHPDEV\win32build, which will contain further subfolders and files. It's not necessary to name your root folder PHPDEV; the only important thing is that win32build and the PHP source tree are both children of the same parent folder.
Next you'll need a compiler. If you've already got Visual C++ .NET you have what you need; otherwise, download Visual C++ Express from Microsoft at http://lab.msdn.microsoft.com/express/.
The installer, once you've downloaded and run it, will display the usual welcome, EULA (End-User License Agreement), and identification dialogs. Read through these screens and proceed using the Next buttons after you've agreed to the terms of the EULA and entered any appropriate information.
Installation location is of course up to you, and a typical installation will work just fine. If you'd like to create a leaner installation, you may deselect the three optional componentsGUI, MSDN, and SQL Server.
The final package is the Platform SDK, also available for download from Microsoft at http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5. The site currently lists three download options: PSDK-x86.exe, PSDK-ia64.exe, and PSDK-amd64.exe. These options refer to x86 compatible 32bit, Intel64bit, and AMD64bit processors respectively. If you're not sure which one applies to your processor, select PSDK-x86.exe, which should work cleanly, albeit less efficiently, with both 64 bit variants.
As before, proceed through the first few screens as you would with any other installer package until you are prompted to select between Typical and Custom installation. A Typical installation includes the Core SDK package, which is sufficient for the purposes of building PHP. Other packages can be deselected by choosing a Custom installation, but if you have the hard disk space to spare, you might as well install it all. The other packages may come in handy later on.
So unless you're byte conscious, select Typical and proceed through the next couple of standard issue screens until the installer begins copying and registering files. This process should take a few minutes so grab some popcorn.
Once installation is complete you'll have a new item on your Start menuMicrosoft Platform SDK for Windows Server 2003 SP1.
</pre>
### 獲取PHP源碼
其實你有很多辦法安裝PHP,最簡單的一種就是從你系統的庫或者源里通過apt-get、yum install之類的命令直接安裝PHP5,這樣做的好處你的系統可能會自動處理一些php在它上面的工作時的一些bug,而且你還可以方便的升級與卸載。這樣做也有缺點,那就是你的PHP版本永遠無法是最新的,通常www.php.net發布數周甚至數月后你才能用上相應的版本。
第二種方法:也是推薦使用的一種方法,那就是自行下載php-x.y.z.tar.gz的源碼包,然后自行編譯安裝。這種包一般都是經過了海量的測試后才發布的,而且非常接近最新beta或者alpha版本。
此外,你還可以snaps.php.net提供的快照包來下載php進行編譯安裝,這個站點每幾個小時便會從源碼庫里打包出一份新的PHP。不過從這取得的包可能會因為某個未經完整測試的代碼提交而使PHP工作不正常。但是如果你想研究下PHP6.0的進展,這里絕對是你方便獲取它的地方。
最后,你可以直接從版本庫中導出此時此刻的源碼。作為一個擴展開發者,從版本庫或者snaps中獲取php看起來并沒有多大的作用,但是如果我們要將這個擴展推送到版本庫中時,便需要熟練的掌握checkout和checkin的步驟了。簽出的地址在上面已經說過了。
## links
* 4 [動手編譯PHP](<4.md>)
* 4.2 [PHP編譯前的config配置](<4.2.md>)
- about
- 開始閱讀
- 目錄
- 1 PHP的生命周期
- 1.讓我們從SAPI開始
- 2.PHP的啟動與終止
- 3.PHP的生命周期
- 4.線程安全
- 5.小結
- 2 PHP變量在內核中的實現
- 1. 變量的類型
- 2. 變量的值
- 3. 創建PHP變量
- 4. 變量的存儲方式
- 5. 變量的檢索
- 6. 類型轉換
- 7. 小結
- 3 內存管理
- 1. 內存管理
- 2. 引用計數
- 3. 總結
- 4 動手編譯PHP
- 1. 編譯前的準備
- 2. PHP編譯前的config配置
- 3. Unix/Linux平臺下的編譯
- 4. 在Win32平臺上編譯PHP
- 5. 小結
- 5 Your First Extension
- 1. 一個擴展的基本結構
- 2. 編譯我們的擴展
- 3. 靜態編譯
- 4. 編寫函數
- 5. 小結
- 6 函數返回值
- 1. 一個特殊的參數:return_value
- 2. 引用與函數的執行結果
- 3. 小結
- 7 函數的參數
- 1. zend_parse_parameters
- 2. Arg Info 與類型綁定
- 3. 小結
- 8 使用HashTable與{數組}
- 1. 數組(C中的)與鏈表
- 2. 操作HashTable的API
- 3. 在內核中操作PHP語言中數組
- 4. 小結
- 9 PHP中的資源類型
- 1. 復合類型的數據——{資源}
- 2. Persistent Resources
- 3. {資源}自有的引用計數
- 4. 小結
- 10 PHP中的面向對象(一)
- 1. zend_class_entry
- 2. 定義一個類
- 3. 定義一個接口
- 4. 類的繼承與接口的實現
- 5. 小結
- 11 PHP中的面向對象(二)
- 1. 生成對象的實例與調用方法
- 2. 讀寫對象的屬性
- 3. 小結
- 12 啟動與終止的那點事
- 2. 小結
- 1. 關于生命周期
- 2. MINFO與phpinfo
- 3. 常量
- 4. PHP擴展中的全局變量
- 5. PHP語言中的超級全局變量
- 6. 小結
- 13 INI設置
- 1. 聲明和訪問ini設置
- 2. 小結
- 2. 小結
- 14 流式訪問
- 1. 概覽
- 2. 打開流
- 3. 訪問流
- 4. 靜態資源操作
- 5. 小結
- 15 流的實現
- 1. php流的表象之下
- 2. 包裝器操作
- 3. 實現一個包裝器
- 4. 操縱
- 5. 檢查
- 6. 小結
- 16 有趣的流
- 1. 上下文
- 2. 過濾器
- 3. 小結
- 17 配置和鏈接
- 1. autoconf
- 2. 庫的查找
- 3. 強制模塊依賴
- 4. Windows方言
- 5. 小結
- 18 擴展生成
- 1. ext_skel
- 2. PECL_Gen
- 3. 小結
- 19 設置宿主環境
- 1. 嵌入式SAPI
- 2. 構建并編譯一個宿主應用
- 3. 通過嵌入包裝重新創建cli
- 4. 老技術新用
- 5. 小結
- 20 高級嵌入式
- 1. 回調到php中
- 2. 錯誤處理
- 3. 初始化php
- 4. 覆寫INI_SYSTEM和INI_PERDIR選項
- 5. 捕獲輸出
- 6. 同時擴展和嵌入
- 7. 小結
- 約定