【问题标题】:Create PHP extension创建 PHP 扩展
【发布时间】:2016-01-25 14:49:03
【问题描述】:

尝试根据manual创建简单的Hello World PHP扩展。

config.m4

PHP_ARG_ENABLE(hello, whether to enable Hello
World support,
[ --enable-hello   Enable Hello World support])
if test "$PHP_HELLO" = "yes"; then
  AC_DEFINE(HAVE_HELLO, 1, [Whether you have Hello World])
  PHP_NEW_EXTENSION(hello, hello.c, $ext_shared)
fi

你好.h

#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1
#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "hello"

PHP_FUNCTION(hello_world);

extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry

#endif

你好.c

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "hello.h"

static function_entry hello_functions[] = {
    PHP_FE(hello_world, NULL)
    {NULL, NULL, NULL}
};

zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_HELLO_WORLD_EXTNAME,
    hello_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
#if ZEND_MODULE_API_NO >= 20010901
    PHP_HELLO_WORLD_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif

PHP_FUNCTION(hello_world)
{
    RETURN_STRING("Hello World", 1);
}

make 抛出错误:

/bin/bash /home/ggg/cpp/php/libtool --mode=compile cc  -I. -I/home/ggg/cpp/php -DPHP_ATOM_INC -I/home/ggg/cpp/php/include -I/home/ggg/cpp/php/main -I/home/ggg/cpp/php -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /home/ggg/cpp/php/hello.c -o hello.lo 
libtool: compile:  cc -I. -I/home/ggg/cpp/php -DPHP_ATOM_INC -I/home/ggg/cpp/php/include -I/home/ggg/cpp/php/main -I/home/ggg/cpp/php -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /home/ggg/cpp/php/hello.c  -fPIC -DPIC -o .libs/hello.o
/home/ggg/cpp/php/hello.c:8:8: error: unknown type name 'function_entry'
 static function_entry hello_functions[] = {
        ^
/home/ggg/cpp/php/hello.c:9:5: warning: braces around scalar initializer
     PHP_FE(hello_world, NULL)
     ^
/home/ggg/cpp/php/hello.c:9:5: warning: (near initialization for 'hello_functions[0]')
/home/ggg/cpp/php/hello.c:9:5: warning: initialization makes integer from pointer without a cast
/home/ggg/cpp/php/hello.c:9:5: warning: (near initialization for 'hello_functions[0]')
/home/ggg/cpp/php/hello.c:9:5: error: initializer element is not computable at load time
/home/ggg/cpp/php/hello.c:9:5: error: (near initialization for 'hello_functions[0]')
/home/ggg/cpp/php/hello.c:9:5: warning: excess elements in scalar initializer
/home/ggg/cpp/php/hello.c:9:5: warning: (near initialization for 'hello_functions[0]')
/home/ggg/cpp/php/hello.c:9:5: warning: excess elements in scalar initializer
/home/ggg/cpp/php/hello.c:9:5: warning: (near initialization for 'hello_functions[0]')
In file included from /usr/include/php5/main/php.h:40:0,
                 from /home/ggg/cpp/php/hello.c:5:
/usr/include/php5/Zend/zend_API.h:71:129: warning: excess elements in scalar initializer
 #define ZEND_FENTRY(zend_name, name, arg_info, flags) { #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },
                                                                                                                                 ^
/usr/include/php5/Zend/zend_API.h:77:38: note: in expansion of macro 'ZEND_FENTRY'
 #define ZEND_FE(name, arg_info)      ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)
                                      ^
/usr/include/php5/main/php.h:353:18: note: in expansion of macro 'ZEND_FE'
 #define PHP_FE   ZEND_FE
                  ^
/home/ggg/cpp/php/hello.c:9:5: note: in expansion of macro 'PHP_FE'
     PHP_FE(hello_world, NULL)
     ^
/usr/include/php5/Zend/zend_API.h:71:129: warning: (near initialization for 'hello_functions[0]')
 #define ZEND_FENTRY(zend_name, name, arg_info, flags) { #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },
                                                                                                                                 ^
/usr/include/php5/Zend/zend_API.h:77:38: note: in expansion of macro 'ZEND_FENTRY'
 #define ZEND_FE(name, arg_info)      ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)
                                      ^
/usr/include/php5/main/php.h:353:18: note: in expansion of macro 'ZEND_FE'
 #define PHP_FE   ZEND_FE
                  ^
/home/ggg/cpp/php/hello.c:9:5: note: in expansion of macro 'PHP_FE'
     PHP_FE(hello_world, NULL)
     ^
/usr/include/php5/Zend/zend_API.h:71:129: warning: excess elements in scalar initializer
 #define ZEND_FENTRY(zend_name, name, arg_info, flags) { #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },
                                                                                                                                 ^
/usr/include/php5/Zend/zend_API.h:77:38: note: in expansion of macro 'ZEND_FENTRY'
 #define ZEND_FE(name, arg_info)      ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)
                                      ^
/usr/include/php5/main/php.h:353:18: note: in expansion of macro 'ZEND_FE'
 #define PHP_FE   ZEND_FE
                  ^
/home/ggg/cpp/php/hello.c:9:5: note: in expansion of macro 'PHP_FE'
     PHP_FE(hello_world, NULL)
     ^
/usr/include/php5/Zend/zend_API.h:71:129: warning: (near initialization for 'hello_functions[0]')
 #define ZEND_FENTRY(zend_name, name, arg_info, flags) { #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },
                                                                                                                                 ^
/usr/include/php5/Zend/zend_API.h:77:38: note: in expansion of macro 'ZEND_FENTRY'
 #define ZEND_FE(name, arg_info)      ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)
                                      ^
/usr/include/php5/main/php.h:353:18: note: in expansion of macro 'ZEND_FE'
 #define PHP_FE   ZEND_FE
                  ^
/home/ggg/cpp/php/hello.c:9:5: note: in expansion of macro 'PHP_FE'
     PHP_FE(hello_world, NULL)
     ^
/home/ggg/cpp/php/hello.c:10:5: warning: braces around scalar initializer
     {NULL, NULL, NULL}
     ^
/home/ggg/cpp/php/hello.c:10:5: warning: (near initialization for 'hello_functions[1]')
/home/ggg/cpp/php/hello.c:10:5: warning: initialization makes integer from pointer without a cast
/home/ggg/cpp/php/hello.c:10:5: warning: (near initialization for 'hello_functions[1]')
/home/ggg/cpp/php/hello.c:10:5: warning: excess elements in scalar initializer
/home/ggg/cpp/php/hello.c:10:5: warning: (near initialization for 'hello_functions[1]')
/home/ggg/cpp/php/hello.c:10:5: warning: excess elements in scalar initializer
/home/ggg/cpp/php/hello.c:10:5: warning: (near initialization for 'hello_functions[1]')
/home/ggg/cpp/php/hello.c:18:5: warning: initialization from incompatible pointer type
     hello_functions,
     ^
/home/ggg/cpp/php/hello.c:18:5: warning: (near initialization for 'hello_module_entry.functions')
Makefile:180: recipe for target 'hello.lo' failed
make: *** [hello.lo] Error 1

【问题讨论】:

  • 你看过那个教程有多久了?建议您在继续之前找到一个更新的
  • 我建议查看一个简单的现代 PHP 扩展并弄清楚它是如何工作的。
  • @Andrea 你有一个很好的现代 PHP 扩展示例的链接吗?

标签: php c++ php-internals


【解决方案1】:

我已尝试过本教程,但在运行 make 时出现错误。 然后,我发现 function_entry 不适用于 PHP 5.4 或 5.5。我google了一下,发现:如果我用zend_function_entry替换function_entry,就可以解决问题。 它对我来说很好用。

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 2012-03-11
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2011-02-25
    • 1970-01-01
    相关资源
    最近更新 更多