linux下的开发php的C++扩展库的文章不少,今天具体讲一下Windows下扩展库C++的开发.
一、开始之前
1. 系统环境:Windows 7
2. 开发工具:VS 2010
3. AP环境:即已经存在的Apache2.2X(VC9)、PHP5.3x(VC9)环境(如果不需要在本机测试php库,这可以不安装AP环境)。
4. PHP源码:去官网http://www.php.net/downloads.php 下载稳定版本的php源码包(因为要编译扩展库,必须要php的源码才能编译),将源码解压到如:d:\php_src 目录下。本示例用的是PHP5.3.14。下载二进制包(如果已经安装了php环境,就可以不用下载),这里主要用到php二进制包中的php5ts.lib,该文件位于php的dev目录。本示例使用的是php-5.3.14-Win32-VC9-x86二进制包。
5. 配置源码:将源码中php_src/win32/build/config.w32.h.in文件拷贝一份到php_src/main/下,并重命名为:config.w32.h。
二、创建项目
1、创建一个空的win32项目(注意:是Win32的 dll 项目工程,)。
2、配置工程属性:
(1)添加附加包含目录:在C/C++的选项中,添加附加包含目录。包含php源码中的几个目录。
如:D:\php_src;D:\php_src\main;D:\php_src\Zend;D:\php_src\TSRM;D:\php_src\win32;
(2)添加预处理器:ZEND_DEBUG=0;ZTS=1;ZEND_WIN32;PHP_WIN32;
(3)添加附加库:php5ts.lib(该库位于php二进制文间包中的dev目录)
三、编写源码示例
1、添加源文件如:Main.cpp和源文件的头文件Main.h。其中文件的内容主要参考了在linux下编写php扩展库,自动生成的文件的内容
Main.h文件内容:
#ifndef PHP_TEST_MAIN_H
#define PHP_TEST_MAIN_H
extern zend_module_entry PHPTest_module_entry; // PHPTest 是该示例的工程名字, PHPTest_module_entry是php扩展库的入口声明
#define phpext_PHPTest_ptr &PHPTest_module_entry
#ifdef PHP_WIN32
#define PHP_PHPTest_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#define PHP_PHPTest_API __attribute__ ((visibility("default")))
#else
#define PHP_PHPTest_API
#endif
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(PHPTest);
PHP_MSHUTDOWN_FUNCTION(PHPTest);
PHP_RINIT_FUNCTION(PHPTest);
PHP_RSHUTDOWN_FUNCTION(PHPTest);
PHP_MINFO_FUNCTION(PHPTest);
// PHP_FUNCTION 用于定义要导出给php调用的函数名称,这里我们定义了3个函数:init_module,test_module, close_module
// PHP_FUNCTION 只用来声明函数的名称,置于函数的参数将在cpp中定义
PHP_FUNCTION(init_module);
PHP_FUNCTION(test_module);
PHP_FUNCTION(close_module);
/*
Declare any global variables you may need between the BEGIN
and END macros here:
ZEND_BEGIN_MODULE_GLOBALS(CSVirusAnalyse)
long global_value;
char *global_string;
ZEND_END_MODULE_GLOBALS(CSVirusAnalyse)
*/
/* In every utility function you add that needs to use variables
in php_CSVirusAnalyse_globals, call TSRMLS_FETCH(); after declaring other
variables used by that function, or better yet, pass in TSRMLS_CC
after the last function argument and declare your utility function
with TSRMLS_DC after the last declared argument. Always refer to
the globals in your function as CSGAVIRUSANALYSIS_G(variable). You are
encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/
#ifdef ZTS
#define PHPTEST_G(v) TSRMG(PHPTest_globals_id, zend_PHPTest_globals *, v)
#else
#define PHPTEST_G(v) (PHPTest_globals.v)
#endif
#endif/* PHP_TEST_MAIN_H*/
编译Mian.cpp文件:
#define PHP_COMPILER_ID "VC9"// 注意:一定要声明该宏,表示使用的lib文件是用VC9编译的 // 声明以下的宏定义解决在编译过程中会发生:error C2466: 不能分配常量大小为0 的数组的错误。 #ifdef PHP_WIN32 #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr)?(expr):1 ] #else #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ] #endif // #include "XXXXX.h" 在以下包含头文件的前面包含要用到的c++ 的stl的头文件,或者你自己写的C++的头文件。 #include <string> using namespace std; extern "C"{ #include "zend_config.w32.h" #include "php.h" #include "ext/standard/info.h" #include "Main.h" } // 声明了扩展库的导出函数列表 zend_function_entry PHPTest_functions[] = { PHP_FE(init_module, NULL) PHP_FE(test_module, NULL) PHP_FE(close_module, NULL) PHP_FE_END }; zend_module_entry PHPTest_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "PHPTest", PHPTest_functions, PHP_MINIT(PHPTest), PHP_MSHUTDOWN(PHPTest), PHP_RINIT(PHPTest), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(PHPTest), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(PHPTest), #if ZEND_MODULE_API_NO >= 20010901 "0.1", /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; ZEND_GET_MODULE(PHPTest); PHP_MINIT_FUNCTION(PHPTest) { /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(PHPTest) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } PHP_RINIT_FUNCTION(PHPTest) { return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(PHPTest) { return SUCCESS; } PHP_MINFO_FUNCTION(PHPTest) { php_info_print_table_start(); php_info_print_table_header(2, "PHPTest support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } // 以下是php导出函数的实现,比如string init_module(string content) PHP_FUNCTION(init_module) { char *content = NULL; // int argc = ZEND_NUM_ARGS(); int content_len; // 这句话便是导出传入参数 if (zend_parse_parameters(argc TSRMLS_CC, "s", &content, &content_len) == FAILURE) return; if(content) { // 这里只是为了测试,直接把传入值返回去。 string strRet = content; // 返回值 RETURN_STRING((char*)strRet.c_str(), 1); } else { php_error(E_WARNING, "init_module: content is NULL"); } } // 以下是int test_module(string content)函数的实现 PHP_FUNCTION(test_module) { char *content = NULL; int argc = ZEND_NUM_ARGS(); int content_len; if (zend_parse_parameters(argc TSRMLS_CC, "s", &content, &content_len) == FAILURE) return; if(content) { int nRet = content_len; RETURN_LONG(nRet); } else { php_error(E_WARNING, "test_module: &content is NULL"); } } // 以下是 void close_module()函数的实现 PHP_FUNCTION(close_module) { if (zend_parse_parameters_none() == FAILURE) { return; } php_printf("close_module successfully\n"); }
ok,编写完以上的文件后,编译一下,将生成的dll文件,拷贝到正常工作的php 的ext文件夹下,并在php.ini上配置,在extension=php_zip.dll后面添加extension=PHPTest.dll。然后重启Apache。
编写php测试 代码
<?php
echo init_module('test init');
echo'<br>';
//输出: test init
echo test_module('test_module');
echo'<br>';
close_module();
?>
运行结果:
test init
11
close_module successfully
四、注意
1、注意你的头文件的包含的顺序。
将你的头文件以及Windows和C++的头文件包含在php头文件的前面
#include "xxxx.h" // 你的头文件 extern "C"{ #include "zend_config.w32.h" #include "php.h" #include "ext/standard/info.h" #include "Main.h" }
2.可能遇到error C2466: 不能分配常量大小为0 的数组
解决方法:
在vc的 c:\program files\microsoft visual studio 8\vc\include\malloc.h 文件中找到: #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ] 将这一行改为: #ifdef PHP_WIN32 #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr)?(expr):1 ] #else #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ] #endif
或者直接在你的cpp文件中定义也可以。
2. 如果遇到2019连接错误,那么通常是没有删除预处理定义中的宏LIBZEND_EXPORTS,