一个最简单的php的C扩展

WBOY
Libérer: 2016-08-08 09:29:58
original
1122 Les gens l'ont consulté

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

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

<span>To</span> use your new extension, you will have to execute the following <span>steps:</span><span>1</span>.  <span>$ </span>cd ..
<span>2</span>.  <span>$ </span>vi ext/andy/config.m4
<span>3</span>.  <span>$ </span>./buildconf
<span>4</span>.  <span>$ </span>./configure --[with|enable]-andy
<span>5</span>.  <span>$ </span>make
<span>6</span>.  <span>$ </span>./sapi/cli/php -f ext/andy/andy.php
<span>7</span>.  <span>$ </span>vi ext/andy/andy.c
<span>8</span>.  <span>$ </span>make

<span>Repeat</span> steps <span>3</span>-<span>6</span><span>until</span> you are satisfied with ext/andy/config.m4 <span>and</span>
step <span>6</span> confirms that your <span><span>module</span><span>is</span><span>compiled</span><span>into</span><span>PHP</span>. <span>Then</span>, <span>start</span><span>writing</span></span>
code <span>and</span> repeat the last two steps as often as necessary.

andy<span>@AndyMacBookPro</span><span>:~/Downloads/php-</span><span>5.4</span>.<span>30</span>/ext<span>$ </span>cd andy/
andy<span>@AndyMacBookPro</span><span>:~/Downloads/php-</span><span>5.4</span>.<span>30</span>/ext/andy<span>$ </span>ll
total <span>64</span>
drwxr-xr-x  <span>11</span> andy  staff   <span>374</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> ./
drwxr-xr-x@ <span>81</span> andy  staff  <span>2754</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> ../
-rw-r--r--   <span>1</span> andy  staff    <span>16</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> .svnignore
-rw-r--r--   <span>1</span> andy  staff     <span>5</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span><span>CREDITS</span>
-rw-r--r--   <span>1</span> andy  staff     <span>0</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span><span>EXPERIMENTAL</span>
-rw-r--r--   <span>1</span> andy  staff  <span>5044</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> andy.c
-rw-r--r--   <span>1</span> andy  staff   <span>496</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> andy.php
-rw-r--r--   <span>1</span> andy  staff  <span>1970</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> config.m4
-rw-r--r--   <span>1</span> andy  staff   <span>282</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> config.w32
-rw-r--r--   <span>1</span> andy  staff  <span>2812</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> php_andy.h
drwxr-xr-x   <span>3</span> andy  staff   <span>102</span><span>2</span><span>6</span><span>11</span><span>:</span><span>42</span> tests/
andy<span>@AndyMacBookPro</span><span>:~/Downloads/php-</span><span>5.4</span>.<span>30</span>/ext/andy$</code>
Copier après la connexion

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

<code>PHP_ARG_WITH(andy, <span>for</span> andy support,
[  <span>--with-andy             Include andy support])</span>dnl Otherwise <span>use</span> enable:

PHP_ARG_ENABLE(andy, whether <span>to</span> enable andy support,
[  <span>--enable-andy           Enable andy support])</span><span>if</span> test <span>"$PHP_ANDY"</span> != <span>"no"</span>; <span>then</span>
  PHP_NEW_EXTENSION(andy, andy.c, $ext_shared)
fi</code>
Copier après la connexion

dnl代表注释

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

<code><span>PHP_FUNCTION(confirm_andy_compiled)</span>;    <span>/* For testing, remove later. */</span><span>PHP_FUNCTION(andy_whoami)</span>;</code>
Copier après la connexion

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

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

<code><span>/* </span><span>{{{ <span>andy</span>_<span>functions</span>[]
 *
 * <span>Every</span><span>user</span><span>visible</span><span>function</span><span>must</span><span>have</span><span>an</span><span>entry</span><span><span>in</span></span><span>andy</span>_<span>functions</span>[]<span>.</span>
 */
<span>const</span><span>zend</span>_<span>function</span>_<span>entry</span><span>andy</span>_<span>functions</span>[] = {
    <span>PHP</span>_<span>FE</span>(<span>confirm</span>_<span>andy</span>_<span>compiled</span>,   <span>NULL</span>)       /* <span>For</span><span>testing</span>, <span>remove</span><span>later.</span> */
    <span>PHP</span>_<span>FE</span>(<span>andy</span>_<span>whoami</span>, <span>NULL</span>)       /* <span>my</span><span>def</span><span>function</span> : <span>whoami.</span> */
    <span>PHP</span>_<span>FE</span>_<span>END</span>  /* <span>Must</span><span>be</span><span>the</span><span>last</span><span>line</span><span><span>in</span></span><span>andy</span>_<span>functions</span>[] */
};
/* }}</span><span>} */</span></code>
Copier après la connexion

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

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

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

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

creating libtool
appending configuration <span>tag</span><span>"CXX"</span><span>to</span> libtool
configure: creating <span>.</span>/config<span>.</span>status
config<span>.</span>status: creating config<span>.</span>h
andy@AndyMacBookPro:~/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy$ make
/bin/sh /Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/libtool <span>--</span>mode<span>=</span>compile cc  <span>-I</span><span>.</span><span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy <span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/include <span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/main <span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy <span>-I</span>/usr/include/php <span>-I</span>/usr/include/php/main <span>-I</span>/usr/include/php/TSRM <span>-I</span>/usr/include/php/Zend <span>-I</span>/usr/include/php/ext <span>-I</span>/usr/include/php/ext/<span>date</span>/lib  <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-c</span> /Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/andy<span>.</span>c <span>-o</span> andy<span>.</span>lo
mkdir <span>.</span>libs
 cc <span>-I</span><span>.</span><span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy <span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/include <span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/main <span>-I</span>/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy <span>-I</span>/usr/include/php <span>-I</span>/usr/include/php/main <span>-I</span>/usr/include/php/TSRM <span>-I</span>/usr/include/php/Zend <span>-I</span>/usr/include/php/ext <span>-I</span>/usr/include/php/ext/<span>date</span>/lib <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-c</span> /Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/andy<span>.</span>c  <span>-fno</span><span>-common</span><span>-DPIC</span><span>-o</span><span>.</span>libs/andy<span>.</span>o
/Users/andy/Downloads/php<span>-</span><span>5.4</span><span>.30</span>/ext/andy/andy<span>.</span>c:<span>187</span>:<span>14</span>: error: use of
      undeclared identifier <span>'null'</span>
        char <span>*</span>arg <span>=</span><span>null</span>;
                    ^
<span>1</span> error generated<span>.</span>
make: <span>***</span><span>[</span>andy<span>.</span>lo<span>]</span><span> Error 1
andy@AndyMacBookPro:~/Downloads/php-5.4.30/ext/andy$</span></code>
Copier après la connexion

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

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

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

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

<span>Build</span> complete.
<span>Don</span><span>'t forget to run '</span>make test<span>'.

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'</span>
flag during linking <span>and</span><span>do</span> at least one of the <span>following:</span>
   - add <span>LIBDIR</span> to the `<span>DYLD_LIBRARY_PATH</span><span>' 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$</span></code>
Copier après la connexion

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

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

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

extension = andy.so

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

然后重启apache:

apachectl restart

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

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

<code><php>echo andy_whoami();
<span>?></span></php></code>
Copier après la connexion

可以看到果然打印出了

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教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal