編寫PHP擴展庫步驟

筆記:

1、下載PHP源碼,然後解壓縮,假設路徑爲:/home/wyq/php-7.3.0,給目錄添加可執行權限,

chmod -R +x ./

2、進入/home/wyq/php-7.3.0/ext/目錄,最好給

然後執行

 ./ext_skel --extname=wyq

其中wyq就是擴展庫的名稱,wyq.so,

3、之後會在ext目錄下生成wyq目錄,進入wyq目錄查看會生成如下文件:

CREDITS  EXPERIMENTAL  config.m4  config.w32  wyq.c  wyq.php  php_wyq.h  tests

4、編寫config.m4文件,把對應的註釋去掉,dnl是註釋的意思

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(hello, for hello support,
dnl Make sure that the comment is aligned:
dnl [  --with-hello             Include hello support])

dnl Otherwise use enable:

PHP_ARG_ENABLE(hello, whether to enable hello support,
Make sure that the comment is aligned:
[  --enable-hello           Enable hello support])

由於wyq庫沒有引用其他的庫,所以這裏把下面的三行註釋去掉

5、編寫wyq.c文件,有一段代碼如下:

PHP_FUNCTION(confirm_wyq_compiled)
{
	char *arg = NULL;
	size_t arg_len, len;
	zend_string *strg;

	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
		return;
	}

	strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "wyq", arg);

	RETURN_STR(strg);
}

這個就是擴展函數 confirm_wyq_compiled

在PHP文件裏面直接調用這個函數就可以了

6、然後進入ext/hello目錄,執行phpize(該命令在編譯PHP的時候會自動生成),

最後就是正常的./configure && make && make install

最終會生成wyq.so文件

7、還有一種編譯方法就是回到源碼根目錄,執行./configure --enable-wyq

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章