php - 用 C 创建 PHP 扩展

标签 php c visual-studio-2010 php-extension

我对用 C 代码创建 PHP 扩展感到震惊。我已经引用了链接并按照他们给出的步骤进行操作:

http://netindonesia.net/blogs/risman/archive/2008/06/15/part-2-writing-php-extension

http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/

我仍然遇到创建扩展的问题。

我在 Windows 7 和 Visual Studio 2010 Professional 中使用 XAMPP 和 PHP 版本 5.4.16 来创建和编译 C++ 代码。

我正在使用以下 C++ 代码:

 #include "stdio.h"
 #include "stdafx.h"
 /* declaration of functions to be exported */
 ZEND_FUNCTION(DoubleUp);

/* compiled function list so Zend knows what's in this module */
zend_function_entry FirstPHPExtModule_functions[] = {
     ZEND_FE(DoubleUp, NULL)
     {NULL, NULL, NULL}
};

/* compiled module information */
zend_module_entry FirstPHPExtModule_module_entry = {
     STANDARD_MODULE_HEADER,
     "FirstPHPExt Module",
     FirstPHPExtModule_functions,
     NULL, NULL, NULL, NULL, NULL,
     NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(FirstPHPExtModule)

/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
   long paramValue = 0;
   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &paramValue) == FAILURE) {
       RETURN_STRING("Bad parameters!", true);
   }
   paramValue *= 2;
   RETURN_LONG(paramValue);
}

但是,我收到如下错误:

 1>php_myclass.cpp(16): error C2065: 'ZEND_DEBUG' : undeclared identifier
 1>
 1>Build FAILED. 

请帮我在Windows 7环境下创建一个PHP扩展。

最佳答案

在项目定义中定义 ZEND_DEBUG=0 ,或项目属性,具体取决于您正在使用的内容。或者,在 stdafx.h 中设置

#ifndef ZEND_DEBUG
#define ZEND_DEBUG 0
#endif

如果您使用的是 Visual Studio

#ifdef _DEBUG
#define ZEND_DEBUG 1
#else
#define ZEND_DEBUG 0
#endif

因此,如果您在 Debug模式下构建,zend 调试代码就会进入,而在发布版本中构建会删除所有 zend 调试代码。

注意:请确保您的项目中还定义了 ZTS=1ZEND_WIN32PHP_WIN32

按照本指南设置正确的环境,以使用 Visual Studio 构建 php 扩展 visual studio build guide 。它是针对 VC2005 的,但过程大致相同。

关于php - 用 C 创建 PHP 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18096522/

相关文章:

c# - Visual Studio 中的 “Go To Definition” 仅显示非项目引用的元数据

PHP-MySQLi 连接随机失败,出现 "Cannot assign requested address"

php - 逻辑复杂的MYSQL选择查询

c - 仅在第二次发送关闭的套接字时管道损坏

c - 将值插入双链表的中间

c - 递归函数查找回文

c++ - 本地禁用填充

php - 为什么一个简单的 PHP unset() 内存测试使用的内存量是 PHP 5.3 的两倍?

php - 安装so文件时PDO_MYSQL配置失败

visual-studio - 有没有办法在 Visual Studio 中删除一行而不剪切它?