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 CREDITSEXPERIMENTAL 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-6until you are satisfied with ext/andy/config.m4 andstep 6 confirms that your moduleiscompiledintoPHP. Then, startwritingcode 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 3742611:42 ./drwxr-xr-x@ 81 andy staff 27542611:42 ../-rw-r--r-- 1 andy staff 162611:42 .svnignore-rw-r--r-- 1 andy staff 52611:42CREDITS-rw-r--r-- 1 andy staff 02611:42EXPERIMENTAL-rw-r--r-- 1 andy staff 50442611:42 andy.c-rw-r--r-- 1 andy staff 4962611:42 andy.php-rw-r--r-- 1 andy staff 19702611:42 config.m4-rw-r--r-- 1 andy staff 2822611:42 config.w32-rw-r--r-- 1 andy staff 28122611:42 php_andy.hdrwxr-xr-x 3 andy staff 1022611: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_FUNCTION(confirm_andy_compiled); /* For testing, remove later. */PHP_FUNCTION(andy_whoami);
其中PHP_FUNCTION(andy_whoami);这一段使我们添加的,夜间是我们将要添加的扩展函数声明部分。
然后我们去修改andy.c,这是函数主体,我们将我们的函数whoami的指针注册到PHP_FE:
/* {{{ andy_functions[] * * Everyuservisiblefunctionmusthaveanentryinandy_functions[]. */constzend_function_entryandy_functions[] = { PHP_FE(confirm_andy_compiled, NULL) /* Fortesting, removelater. */ PHP_FE(andy_whoami, NULL) /* mydeffunction : whoami. */ PHP_FE_END /* Mustbethelastlineinandy_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-x27 andy staff 9182614:29./drwxr-xr-x@ 81 andy staff 27542611:42../-rw-r--r--1 andy staff 162611:42.svnignore-rw-r--r--1 andy staff 52611:42 CREDITS-rw-r--r--1 andy staff 02611:42 EXPERIMENTAL-rw-r--r--1 andy staff 56072614:29 Makefile.global-rw-r--r--1 andy staff 798512614:29 acinclude.m4-rw-r--r--1 andy staff 3101752614:29 aclocal.m4-rw-r--r--1 andy staff 54152614:27 andy.c-rw-r--r--1 andy staff 4962611:42 andy.phpdrwxr-xr-x5 andy staff 1702614:29 autom4te.cache/drwxr-xr-x6 andy staff 2042614:29 build/-rwxr-xr-x1 andy staff 448932614:29 config.guess*-rw-r--r--1 andy staff 15982614:29 config.h.in-rw-r--r--1 andy staff 18682611:57 config.m4-rw-r--r--1 andy staff 19702611:42 config.m4.bak-rwxr-xr-x1 andy staff 333992614:29 config.sub*-rw-r--r--1 andy staff 2822611:42 config.w32-rwxr-xr-x1 andy staff 4377902614:29 configure*-rw-r--r--1 andy staff 46902614:29 configure.in-rw-r--r--1 andy staff 02614:29 install-sh-rw-r--r--1 andy staff 1997282614:29 ltmain.sh-rw-r--r--1 andy staff 02614:29 missing-rw-r--r--1 andy staff 02614:29 mkinstalldirs-rw-r--r--1 andy staff 28392612:04 php_andy.h-rw-r--r--1 andy staff 795032614:29 run-tests.phpdrwxr-xr-x3 andy staff 1022611: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 -cand-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.4or 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] Error1andy@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/modulesandy.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 anddo 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 2722614:33 ./drwxr-xr-x 3 root wheel 1021202014 ../-rwxr-xr-x 1 root wheel 104962614:33 andy.so*-rwxr-xr-x 1 root wheel 1727327232014 apc.so*-rwxr-xr-x 1 root wheel 51424712014 mssql.so*-rwxr-xr-x 1 root wheel 30084722014 pdo_odbc.so*-rwxr-xr-x 1 root wheel 2556566232014 redis.so*-rwxr-xr-x 1 root wheel 200880922 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脚本来调用我们写的扩展函数:
可以看到果然打印出了
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扩展教学了。
以上就介绍了一个最简单的php的C扩展,包括了方面的内容,希望对PHP教学有兴趣的朋友有所帮助。