欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个最简单的php的C扩展

程序员文章站 2022-03-21 13:29:10
...
要编写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 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.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-6until you are satisfied with ext/andy/config.m4 and
step 6 confirms that your moduleiscompiledintoPHP. Then, startwriting
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   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.h
drwxr-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_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[]
 *
 * 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$ 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-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.php
drwxr-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.php
drwxr-xr-x3 andy  staff     1022611: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 -cand-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.4or 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] Error1
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/modulesandy.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 anddo 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     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脚本来调用我们写的扩展函数:

echo