扩展下载后只有源码,需要举行编译生成.so扩展文件后才可以被PHP使用,config.m4和config.w32这两个文件就是在后续执行phpize阶段会使用到的编译配置文件。
m4是一种宏处理文件,主要由PHP_ARG_WITH和PHP_ARG_ENABLE两部分构成,一般使用第二部分即可,用于开启指定的扩展。这样在编译阶段,就会判断$PHP_PHP_HELLO变量不是no,从而执行此扩展的编译。
其中dnl宏会删除本行多余的字符,可以简朴的明白为表明,如下所示,如果需要编译php_hello这个扩展,把PHP_ARG_ENABLE部分最前面的dnl宏都去掉即可
dnl If your extension references something external, use with:dnl PHP_ARG_WITH(php_hello, for php_hello support,dnl Make sure that the comment is aligned:dnl [ --with-php_hello Include php_hello support])dnl Otherwise use enable:dnl PHP_ARG_ENABLE(php_hello, whether to enable php_hello support,dnl Make sure that the comment is aligned:dnl [ --enable-php_hello Enable php_hello support])if test "$PHP_PHP_HELLO" != "no"; then PHP_NEW_EXTENSION(php_hello, php_hello.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)fi(3) 扩展头文件
找到PHP_ARG_ENABLE这段代码,删掉前面的dnl宏,如下所示
# 修改前dnl PHP_ARG_ENABLE(print_hello, whether to enable print_hello support,dnl Make sure that the comment is aligned:dnl [ --enable-print_hello Enable print_hello support])# 修改后PHP_ARG_ENABLE(print_hello, whether to enable print_hello support,Make sure that the comment is aligned:[ --enable-print_hello Enable print_hello support])4、修改print_hello.c文件
找到PHP_FUNCTION(表示定义的扩展函数),在如下confirm_print_hello_compiled函数中添加一句输出hello world的代码
PHP_FUNCTION(confirm_print_hello_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.", "print_hello", arg); // 新增下面输出hello world的代码 php_printf("hello world!\n"); RETURN_STR(strg);}5、编译扩展
通过执行以下命令执行对扩展的编译处理
cd print_hellophpize./configure --with-php-config=/usr/bin/php-configmakesudo make执行make命令成功后如下所示