PHP的C/C++擴展(一)

因為個性化推薦系統的開發,是架構組與策略組的跨小團隊合作,所以策略方不愿意公開自己的代碼,所以就只能采用我們提供系統功能,對方提供策略部分.so文件來實現(策略團隊以C/C++作為主要語言)。

一、ext_skel 腳本

PHP 擴展由幾個文件組成,這些文件對所有擴展來說都是通用的。不同擴展之間,這些文件的很多細節是相似的,只是要費力去復制每個文件的內容。幸運的是,有腳本可以做所有的初始化工作,名為 ext_skel,自 PHP 4.0 起與其一起分發。

不帶參數運行 ext_skel 在 PHP 5.6.24(最新正式版本) 中會產生以下輸出:

./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

 --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-svn
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled

通常來說,開發一個新擴展時,僅需關注的參數是 --extname 和 --no-help。除非已經熟悉擴展的結構,不要想去使用 --no-help; 指定此參數會造成 ext_skel 在生成文件里省略很多有用的注釋。剩下的 --extname 會將擴展的名稱傳給 ext_skel。"name" 是一個全為小寫字母的標識符,僅包含字母和下劃線,在 PHP 發行包的 ext/ 文件夾下是唯一的。*** 官網說了:其它參數不用明白,也不要嘗試。 ***

執行創建edutest1

 ? zhangxuefeng@zhangxuefengdeMac-mini  ~/Developer/php-5.6.24/ext  ./ext_skel --extname=edutest1
Creating directory edutest1
Creating basic files: config.m4 config.w32 .gitignore edutest1.c php_edutest1.h CREDITS EXPERIMENTAL tests/001.phpt edutest1.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/edutest1/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-edutest1
5.  $ make
6.  $ ./sapi/cli/php -f ext/edutest1/edutest1.php
7.  $ vi ext/edutest1/edutest1.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/edutest1/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

二、與 UNIX 構建系統交互: config.m4

擴展的 config.m4 文件告訴 UNIX 構建系統哪些擴展 configure 選項是支持的,你需要哪些擴展庫,以及哪些源文件要編譯成它的一部分。

autoconf 語法簡介

config.m4 文件使用 GNU autoconf 語法編寫。簡而言之,就是用強大的宏語言增強的 shell 腳本。注釋用字符串 dnl 分隔,字符串則放在左右方括號中間(例如,[ 和 ])。字符串可按需要多次嵌套引用。

根據需要修改config.m4

  1. 切換到ext/edutest1/目錄。
  2. vi config.m4
  3. 將下面的第一、三行取消注釋,并刪掉第二行:
16 dnl PHP_ARG_ENABLE(edutest1, whether to enable edutest1 support,
 17 dnl Make sure that the comment is aligned:
 18 dnl [  --enable-edutest1           Enable edutest1 support])
修改為:
16 PHP_ARG_ENABLE(edutest1, whether to enable edutest1 support,
 17     [  --enable-edutest1           Enable edutest1 support])
  1. PHP_SUBST一行的注釋打開:
19 if test "$PHP_EDUTEST1" != "no"; then
……
59   PHP_SUBST(EDUTEST1_SHARED_LIBADD)
60
61   PHP_NEW_EXTENSION(edutest1, edutest1.c, $ext_shared)
62 fi

簡要說明:

宏PHP_ARG_ENABLE,含有三個參數:

第一個參數,extest1./configure建立了名為enable-edutest1的選項
第二個參數將會在./configure命令處理到該擴展的配置文件時,顯示該參數的內容
第三個參數是./configure命令的幫助,在使用./configure --help的時候顯示

宏PHP_NEW_EXTENSION

該宏聲明了擴展的模塊和必須要編譯作為擴展一部分的源碼文件。如果需要多個源文件,則使用空格分隔,第三個參數$ext_shared與調用PHP_SUBST(EDUTEST1_SHARED_LIBADD)有關。

三、phpize、配置、編譯

因為我的Mac上已經自帶了PHP的環境,就不采取全套PHP源碼編譯的方法了。使用命令行工具phpize對擴展進行編譯。

phpize 命令是用來準備 PHP 擴展庫的編譯環境的工具。如果系統中沒有 phpize 命令并且使用了預編譯的包(例如 RPM),那要安裝 PHP 包相應的開發版本,此版本通常包含了 phpize 命令以及相應的用于編譯 PHP 及其擴展庫的頭文件。使用 phpize --help 命令可以顯示此命令用法。

使用root權限執行phpize:

 zhangxuefeng@zhangxuefengdeMac-mini  ~/Developer/php-5.6.24/ext/edutest1  sudo /usr/bin/phpize
Password:
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212

configure,需要使用php-config工具:

php-config 是一個簡單的命令行腳本用于獲取所安裝的 PHP 配置的信息。在編譯擴展時,如果安裝有多個 PHP 版本,可以在配置時用 --with-php-config 選項來指定使用哪一個版本編譯,該選項指定了相對應的 php-config 腳本的路徑。

 ? zhangxuefeng@zhangxuefengdeMac-mini  ~/Developer/php-5.6.24/ext/edutest1  sudo ./configure --enable-edutest1 --with-php-config=/usr/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
……

Make 編譯:

 zhangxuefeng@zhangxuefengdeMac-mini  ~/Developer/php-5.6.24/ext/edutest1  sudo make
Password:
/bin/sh /Users/zhangxuefeng/Developer/php-5.6.24/ext/edutest1/libtool --mode=compile cc  -I. -I/Users/zhangxuefeng/Developer/php-5.6.24/ext/edutest1 -DPHP_ATOM_INC -I/Users/zhangxuefeng/Developer/php-5.6.24/ext/edutest1/include -I/Users/zhangxuefeng/Developer/php-5.6.24/ext/edutest1/main -I/Users/zhangxuefeng/Developer/php-5.6.24/ext/edutest1 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /Users/zhangxuefeng/Developer/php-5.6.24/ext/edutest1/edutest1.c -o edutest1.lo
mkdir .libs
……
……
……
Build complete.
Don't forget to run 'make test'.

Make Install:

 zhangxuefeng@zhangxuefengdeMac-mini  ~/Developer/php-5.6.24/ext/edutest1  sudo make install
Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20121212/

添加完整路徑到php.ini中:

 zhangxuefeng@zhangxuefengdeMac-mini sudo vim /etc/php.ini
……
899 extension=/usr/lib/php/extensions/no-debug-non-zts-20121212/edutest1.so
……

四、重啟HTTP服務:

phpinfo截圖.png

成功!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容