一个最简单的php的C扩展

要编写php扩展,我们可以先下载一个php的版本的源码,然后进入php的ext目录中,例如我本地是php5.4。

andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext$ ./ext_skel --extname=andy
Creating directory andy
Creating basic files: config.m4 config.w32 .svnignore andy.c php_andy.h CREDITS EXPERIMENTAL tests/001.phpt andy.php [done].

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

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

Repeat steps 3-6 until you are satisfied with ext/andy/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.

andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext$ cd andy/
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ ll
total 64
drwxr-xr-x  11 andy  staff   374  2  6 11:42 ./
drwxr-xr-x@ 81 andy  staff  2754  2  6 11:42 ../
-rw-r--r--   1 andy  staff    16  2  6 11:42 .svnignore
-rw-r--r--   1 andy  staff     5  2  6 11:42 CREDITS
-rw-r--r--   1 andy  staff     0  2  6 11:42 EXPERIMENTAL
-rw-r--r--   1 andy  staff  5044  2  6 11:42 andy.c
-rw-r--r--   1 andy  staff   496  2  6 11:42 andy.php
-rw-r--r--   1 andy  staff  1970  2  6 11:42 config.m4
-rw-r--r--   1 andy  staff   282  2  6 11:42 config.w32
-rw-r--r--   1 andy  staff  2812  2  6 11:42 php_andy.h
drwxr-xr-x   3 andy  staff   102  2  6 11:42 tests/
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$

然后编辑其中的 config.m4,改变为下面这样:

PHP_ARG_WITH(andy, for andy support,
[  --with-andy             Include andy support])

dnl Otherwise use enable:

PHP_ARG_ENABLE(andy, whether to enable andy support,
[  --enable-andy           Enable andy support])

if test "$PHP_ANDY" != "no"; then
  PHP_NEW_EXTENSION(andy, andy.c, $ext_shared)
fi

dnl代表注释

然后我们修改头文件:php_andy.h

PHP_FUNCTION(confirm_andy_compiled);    /* For testing, remove later. */
PHP_FUNCTION(andy_whoami);

其中PHP_FUNCTION(andy_whoami);这一段使我们添加的,夜间是我们将要添加的扩展函数声明部分。

然后我们去修改andy.c,这是函数主体,我们将我们的函数whoami的指针注册到PHP_FE:

/* {{{ andy_functions[]
 *
 * Every user visible function must have an entry in andy_functions[].
 */
const zend_function_entry andy_functions[] = {
    PHP_FE(confirm_andy_compiled,   NULL)       /* For testing, remove later. */
    PHP_FE(andy_whoami, NULL)       /* my def function : whoami. */
    PHP_FE_END  /* Must be the last line in andy_functions[] */
};
/* }}} */

之后我们来编辑功能部分,andy.c,在最后部分添加这段代码:

PHP_FUNCTION(andy_whoami){
    char *arg = null;
    int arg_len, len;
    char *strg;
    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == false){
        return;
    }
    php_printf("I'm andy,It's nice to meet you. wish we could be happy together :-).");
    RETURN_TRUE;
}

之后我们保存退出,之后在目录中我们应该一次进行phpize,./configure,make,make install:

andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ ll
total 2448
drwxr-xr-x  27 andy  staff     918  2  6 14:29 ./
drwxr-xr-x@ 81 andy  staff    2754  2  6 11:42 ../
-rw-r--r--   1 andy  staff      16  2  6 11:42 .svnignore
-rw-r--r--   1 andy  staff       5  2  6 11:42 CREDITS
-rw-r--r--   1 andy  staff       0  2  6 11:42 EXPERIMENTAL
-rw-r--r--   1 andy  staff    5607  2  6 14:29 Makefile.global
-rw-r--r--   1 andy  staff   79851  2  6 14:29 acinclude.m4
-rw-r--r--   1 andy  staff  310175  2  6 14:29 aclocal.m4
-rw-r--r--   1 andy  staff    5415  2  6 14:27 andy.c
-rw-r--r--   1 andy  staff     496  2  6 11:42 andy.php
drwxr-xr-x   5 andy  staff     170  2  6 14:29 autom4te.cache/
drwxr-xr-x   6 andy  staff     204  2  6 14:29 build/
-rwxr-xr-x   1 andy  staff   44893  2  6 14:29 config.guess*
-rw-r--r--   1 andy  staff    1598  2  6 14:29 config.h.in
-rw-r--r--   1 andy  staff    1868  2  6 11:57 config.m4
-rw-r--r--   1 andy  staff    1970  2  6 11:42 config.m4.bak
-rwxr-xr-x   1 andy  staff   33399  2  6 14:29 config.sub*
-rw-r--r--   1 andy  staff     282  2  6 11:42 config.w32
-rwxr-xr-x   1 andy  staff  437790  2  6 14:29 configure*
-rw-r--r--   1 andy  staff    4690  2  6 14:29 configure.in
-rw-r--r--   1 andy  staff       0  2  6 14:29 install-sh
-rw-r--r--   1 andy  staff  199728  2  6 14:29 ltmain.sh
-rw-r--r--   1 andy  staff       0  2  6 14:29 missing
-rw-r--r--   1 andy  staff       0  2  6 14:29 mkinstalldirs
-rw-r--r--   1 andy  staff    2839  2  6 12:04 php_andy.h
-rw-r--r--   1 andy  staff   79503  2  6 14:29 run-tests.php
drwxr-xr-x   3 andy  staff     102  2  6 11:42 tests/
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ ./configure
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
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... i386-apple-darwin13.4.0
checking host system type... i386-apple-darwin13.4.0
checking target system type... i386-apple-darwin13.4.0
checking for PHP prefix... /usr
checking for PHP includes... -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
checking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20100525
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking for andy support... yes, shared
checking whether to enable andy support... yes, shared
checking for ld used by cc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... no
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 196608
checking command to parse /usr/bin/nm output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking for dsymutil... dsymutil
checking for nmedit... nmedit
checking for -single_module linker flag... yes
checking for -exported_symbols_list linker flag... yes
checking if cc supports -fno-rtti -fno-exceptions... yes
checking for cc option to produce PIC... -fno-common
checking if cc PIC flag -fno-common works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin13.4.0 dyld
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ make
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/andy/libtool --mode=compile cc  -I. -I/Users/andy/Downloads/php-5.4.30/ext/andy -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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/andy/Downloads/php-5.4.30/ext/andy/andy.c -o andy.lo
mkdir .libs
 cc -I. -I/Users/andy/Downloads/php-5.4.30/ext/andy -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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/andy/Downloads/php-5.4.30/ext/andy/andy.c  -fno-common -DPIC -o .libs/andy.o
/Users/andy/Downloads/php-5.4.30/ext/andy/andy.c:187:14: error: use of
      undeclared identifier 'null'
        char *arg = null;
                    ^
1 error generated.
make: *** [andy.lo] Error 1
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$

我发现我的源码有问题了,应该大写NULL,于是回去修改之,修改之后重新编译成功:

andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ make
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/andy/libtool --mode=compile cc  -I. -I/Users/andy/Downloads/php-5.4.30/ext/andy -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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/andy/Downloads/php-5.4.30/ext/andy/andy.c -o andy.lo
mkdir .libs
 cc -I. -I/Users/andy/Downloads/php-5.4.30/ext/andy -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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/andy/Downloads/php-5.4.30/ext/andy/andy.c  -fno-common -DPIC -o .libs/andy.o
/Users/andy/Downloads/php-5.4.30/ext/andy/andy.c:187:14: error: use of
      undeclared identifier 'null'
        char *arg = null;
                    ^
1 error generated.
make: *** [andy.lo] Error 1
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ make
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/andy/libtool --mode=compile cc  -I. -I/Users/andy/Downloads/php-5.4.30/ext/andy -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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/andy/Downloads/php-5.4.30/ext/andy/andy.c -o andy.lo
 cc -I. -I/Users/andy/Downloads/php-5.4.30/ext/andy -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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/andy/Downloads/php-5.4.30/ext/andy/andy.c  -fno-common -DPIC -o .libs/andy.o
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/andy/libtool --mode=link cc -DPHP_ATOM_INC -I/Users/andy/Downloads/php-5.4.30/ext/andy/include -I/Users/andy/Downloads/php-5.4.30/ext/andy/main -I/Users/andy/Downloads/php-5.4.30/ext/andy -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   -o andy.la -export-dynamic -avoid-version -prefer-pic -module -rpath /Users/andy/Downloads/php-5.4.30/ext/andy/modules  andy.lo
cc ${wl}-flat_namespace ${wl}-undefined ${wl}suppress -o .libs/andy.so -bundle  .libs/andy.o
dsymutil .libs/andy.so || :
creating andy.la
(cd .libs && rm -f andy.la && ln -s ../andy.la andy.la)
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/andy/libtool --mode=install cp ./andy.la /Users/andy/Downloads/php-5.4.30/ext/andy/modules
cp ./.libs/andy.so /Users/andy/Downloads/php-5.4.30/ext/andy/modules/andy.so
cp ./.libs/andy.lai /Users/andy/Downloads/php-5.4.30/ext/andy/modules/andy.la
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/andy/Downloads/php-5.4.30/ext/andy/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ make install
Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20100525/
cp: /usr/lib/php/extensions/no-debug-non-zts-20100525/#INST@3154#: Permission denied
make: *** [install-modules] Error 1
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ sudo make install
Password:
/bin/sh /Users/andy/Downloads/php-5.4.30/ext/andy/libtool --mode=install cp ./andy.la /Users/andy/Downloads/php-5.4.30/ext/andy/modules
cp ./.libs/andy.so /Users/andy/Downloads/php-5.4.30/ext/andy/modules/andy.so
cp ./.libs/andy.lai /Users/andy/Downloads/php-5.4.30/ext/andy/modules/andy.la
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/andy/Downloads/php-5.4.30/ext/andy/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20100525/
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$

我们可以到这个目录看见自己的so扩展已经在这里了:

andy@AndyMacBookPro:/usr/lib/php/extensions/no-debug-non-zts-20100525$ ll
total 1440
drwxr-xr-x  8 root  wheel     272  2  6 14:33 ./
drwxr-xr-x  3 root  wheel     102  1 20  2014 ../
-rwxr-xr-x  1 root  wheel   10496  2  6 14:33 andy.so*
-rwxr-xr-x  1 root  wheel  172732  7 23  2014 apc.so*
-rwxr-xr-x  1 root  wheel   51424  7  1  2014 mssql.so*
-rwxr-xr-x  1 root  wheel   30084  7  2  2014 pdo_odbc.so*
-rwxr-xr-x  1 root  wheel  255656  6 23  2014 redis.so*
-rwxr-xr-x  1 root  wheel  200880  9 22 09:37 xdebug.so*
andy@AndyMacBookPro:/usr/lib/php/extensions/no-debug-non-zts-20100525$

然后最后一步,我们需要将我们编译出的.so文件加入我们的php.ini中:

extension = andy.so

因为/usr/lib/php/extensions/no-debug-non-zts-20100525这个目录是我本地php的默认扩展读取目录,所以在php.ini配置的时候不需要在写入具体路径。

然后重启apache:

apachectl restart

之后我们可以看到我们的扩展已经出现在了phpinfo()中了。

然后我们编写一个简单的php脚本来调用我们写的扩展函数:

<php
echo andy_whoami();
?>

可以看到果然打印出了

I’m andy,It’s nice to meet you. wish we could be happy together :-).1

后面的.1是因为RETURN_TRUE造成的,可以去掉RETURN_TRUE就不会出现那个1了。

所有以上代码均参考自:http://rango.swoole.com/archives/152,谢谢Rango,才有了我第一个php的C扩展,这个也是我见过最简单有效的C扩展教程了。

展开阅读全文

没有更多推荐了,返回首页