一个最简单的php的C扩展

来源:互联网 发布:弹簧重量计算软件 编辑:程序博客网 时间:2024/05/04 06:57

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

andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext$ ./ext_skel --extname=andyCreating directory andyCreating 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.m43.  $ ./buildconf4.  $ ./configure --[with|enable]-andy5.  $ make6.  $ ./sapi/cli/php -f ext/andy/andy.php7.  $ vi ext/andy/andy.c8.  $ makeRepeat steps 3-6 until you are satisfied with ext/andy/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode 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$ lltotal 64drwxr-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.hdrwxr-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$ phpizeConfiguring for:PHP Api Version:         20100412Zend Module Api No:      20100525Zend Extension Api No:   220100525andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ lltotal 2448drwxr-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.phpdrwxr-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.phpdrwxr-xr-x   3 andy  staff     102  2  6 11:42 tests/andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ ./configurechecking for grep that handles long lines and -e... /usr/bin/grepchecking for egrep... /usr/bin/grep -Echecking for a sed that does not truncate output... /usr/bin/sedchecking for cc... ccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables...checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether cc accepts -g... yeschecking for cc option to accept ISO C89... none neededchecking how to run the C preprocessor... cc -Echecking for icc... nochecking for suncc... nochecking whether cc understands -c and -o together... yeschecking for system library directory... libchecking if compiler supports -R... nochecking if compiler supports -Wl,-rpath,... yeschecking build system type... i386-apple-darwin13.4.0checking host system type... i386-apple-darwin13.4.0checking target system type... i386-apple-darwin13.4.0checking for PHP prefix... /usrchecking 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/libchecking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20100525checking for PHP installed headers prefix... /usr/include/phpchecking if debug is enabled... nochecking if zts is enabled... nochecking for re2c... noconfigure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.checking for gawk... nochecking for nawk... nochecking for awk... awkchecking if awk is broken... nochecking for andy support... yes, sharedchecking whether to enable andy support... yes, sharedchecking for ld used by cc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ldchecking if the linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... nochecking for /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -rchecking for BSD-compatible nm... /usr/bin/nmchecking whether ln -s works... yeschecking how to recognize dependent libraries... pass_allchecking for ANSI C header files... yeschecking for sys/types.h... yeschecking for sys/stat.h... yeschecking for stdlib.h... yeschecking for string.h... yeschecking for memory.h... yeschecking for strings.h... yeschecking for inttypes.h... yeschecking for stdint.h... yeschecking for unistd.h... yeschecking dlfcn.h usability... yeschecking dlfcn.h presence... yeschecking for dlfcn.h... yeschecking the maximum length of command line arguments... 196608checking command to parse /usr/bin/nm output from cc object... okchecking for objdir... .libschecking for ar... archecking for ranlib... ranlibchecking for strip... stripchecking for dsymutil... dsymutilchecking for nmedit... nmeditchecking for -single_module linker flag... yeschecking for -exported_symbols_list linker flag... yeschecking if cc supports -fno-rtti -fno-exceptions... yeschecking for cc option to produce PIC... -fno-commonchecking if cc PIC flag -fno-common works... yeschecking if cc static flag -static works... nochecking if cc supports -c -o file.o... yeschecking whether the cc linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... yeschecking dynamic linker characteristics... darwin13.4.0 dyldchecking how to hardcode library paths into programs... immediatechecking whether stripping libraries is possible... yeschecking if libtool supports shared libraries... yeschecking whether to build shared libraries... yeschecking whether to build static libraries... nocreating libtoolappending configuration tag "CXX" to libtoolconfigure: creating ./config.statusconfig.status: creating config.handy@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.lomkdir .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 1andy@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.lomkdir .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 1andy@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.locc ${wl}-flat_namespace ${wl}-undefined ${wl}suppress -o .libs/andy.so -bundle  .libs/andy.odsymutil .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/modulescp ./.libs/andy.so /Users/andy/Downloads/php-5.4.30/ext/andy/modules/andy.socp ./.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/modulesIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify 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 executionSee any operating system documentation about shared libraries formore 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 installInstalling shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20100525/cp: /usr/lib/php/extensions/no-debug-non-zts-20100525/#INST@3154#: Permission deniedmake: *** [install-modules] Error 1andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$ sudo make installPassword:/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/modulescp ./.libs/andy.so /Users/andy/Downloads/php-5.4.30/ext/andy/modules/andy.socp ./.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/modulesIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify 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 executionSee any operating system documentation about shared libraries formore 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$ lltotal 1440drwxr-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脚本来调用我们写的扩展函数:

<phpecho 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扩展教程了。

0 0
原创粉丝点击