jackyrar 发表于 2015-11-17 10:56:08

c++开发php5 扩展

  



1,准备开发环境  
  源码安装nginx, php, mysql
  nginx安装路径: /opt/server/nginx 源码路径: /home/leon/Downloads/nginx-1.5.6
  php路径: /opt/server/php 源码路径: /home/leon/Downloads/php-5.3.10
  mysql路径: /opt/server/mysql 源码路径: /home/leon/Downloads/mysql-5.1.63
  2,开发PHP扩展
  


  a) 创建扩展工程
  cd /home/leon/Downloads/php-5.3.10/ext/
  ./ext_skel --extname=php5cpp   
  cd php5cpp/
  mv php5cpp.c php5cpp.cpp
  
  b) config.m4
  
  

PHP_ARG_ENABLE(php5cpp, whether to enable php5cpp support,
Make sure that the comment is aligned:
[--enable-php5cpp         Enable php5cpp support])
if test "$PHP_PHP5CPP" != "no"; then
PHP_REQUIRE_CXX()
PHP_SUBST(PHP5CPP_SHARED_LIBADD)
PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)
PHP_NEW_EXTENSION(php5cpp, php5cpp.cpp, $ext_shared)
fi


  
  c)php_php5cpp.h
  

#ifndef PHP_PHP5CPP_H
#define PHP_PHP5CPP_H
#define PHP_VEHICLES_EXTNAME"PHP5CPP"
#define PHP_VEHICLES_EXTVER   "0.1"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#ifdef ZTS
#include "TSRM.h"
#endif
}
extern zend_module_entry php5cpp_module_entry;
#define phpext_php5cpp_ptr &php5cpp_module_entry
#ifdef PHP_WIN32
#define PHP_PHP5CPP_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#define PHP_PHP5CPP_API __attribute__ ((visibility("default")))
#else
#define PHP_PHP5CPP_API
#endif

PHP_MINIT_FUNCTION(php5cpp);
PHP_MSHUTDOWN_FUNCTION(php5cpp);
PHP_RINIT_FUNCTION(php5cpp);
PHP_RSHUTDOWN_FUNCTION(php5cpp);
PHP_MINFO_FUNCTION(php5cpp);
PHP_FUNCTION(confirm_php5cpp_compiled);
PHP_FUNCTION(cpp_test_1);
#ifdef ZTS
#define PHP5CPP_G(v) TSRMG(php5cpp_globals_id, zend_php5cpp_globals *, v)
#else
#define PHP5CPP_G(v) (php5cpp_globals.v)
#endif
#endif



  
  d) php5cpp.cpp
  

#include "php_php5cpp.h"
static int le_php5cpp;
const zend_function_entry php5cpp_functions[] = {
PHP_FE(confirm_php5cpp_compiled,NULL)
PHP_FE(cpp_test_1, NULL)
PHP_FE_END
};
zend_module_entry php5cpp_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"php5cpp",
php5cpp_functions,
PHP_MINIT(php5cpp),
PHP_MSHUTDOWN(php5cpp),
PHP_RINIT(php5cpp),/* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(php5cpp),/* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(php5cpp),
#if ZEND_MODULE_API_NO >= 20010901
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_PHP5CPP
ZEND_GET_MODULE(php5cpp)
#endif
PHP_MINIT_FUNCTION(php5cpp)
{
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(php5cpp)
{
return SUCCESS;
}

PHP_RINIT_FUNCTION(php5cpp)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(php5cpp)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(php5cpp)
{
php_info_print_table_start();
php_info_print_table_header(2, "php5cpp support", "enabled");
php_info_print_table_end();
}

PHP_FUNCTION(confirm_php5cpp_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "php5cpp", arg);
RETURN_STRINGL(strg, len, 0);
}
PHP_FUNCTION (cpp_test_1)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "php5cpp", arg);
RETURN_STRINGL(strg, len, 0);
}



  
  e) 编译安装
  phpize
  ./configure --with-php-config=/opt/server/php/bin/php-config
  make
  sudo make install
  


  f) 修改php.ini
  sudo service php-fpm stop
  sudo gedit /opt/server/php/etc/php.ini
  加入 extension=php5cpp.so
  sudo service php-fpm start
  


  


  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: c++开发php5 扩展