Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的C函数工作得很好,但PHP扩展出错了?_C_Shared Libraries_Php Extension - Fatal编程技术网

为什么我的C函数工作得很好,但PHP扩展出错了?

为什么我的C函数工作得很好,但PHP扩展出错了?,c,shared-libraries,php-extension,C,Shared Libraries,Php Extension,我需要在PHP中调用一个.so共享库,所以我使用“dlopen”和“dlsym”编写了C代码来实现这一点,它可以工作 样本h: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dlfcn.h> typedef int (*libfunc) (char *msg, int msglen); int sfunc(char *msg, int msglen);

我需要在PHP中调用一个.so共享库,所以我使用“dlopen”和“dlsym”编写了C代码来实现这一点,它可以工作

样本h:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

typedef int (*libfunc) (char *msg, int msglen);

int
sfunc(char *msg, int msglen);
它总是返回0

然后我写了一个PHP扩展作为包装器,它接受PHP参数并调用C代码

php_sample.c:

...
#include "sample.h"
...
PHP_FUNCTION(s_func)
{
    char *msg;
    long msglen;
    int result;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &msg, &msglen) == FAILURE) {
        RETURN_NULL();
    }

    result = sfunc(msg, (int) msglen);

    RETURN_LONG(result);
}
然后我将扩展(编译为.so)添加到PHP并测试PHP函数:

php -r "echo s_func("somestring");"
调用总是失败,只需返回非零的内容

为什么??有什么区别吗


注意:我在另一台计算机上测试了这个,它可以工作。那么环境方面有什么问题吗?

注意:系统Ubuntu 16.04,使用PHP7.0.15-0ubuntu0.16.04.4(cli)(NTS)和gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.4),两台测试的计算机都是相同的。
PHP_函数(s_func)
-类型缺失。这不是应该是
PHP_函数(libfunc s_func)
?我对PHP扩展一无所知,但在
PHP-r“echo s_func(“somestring”);“
,你不需要两个参数吗?@Nguaial请看,在向PHP函数传递字符串时,使用类型说明符“s”和两个局部变量来捕捉它,所以一个参数就可以了。顺便说一句,php命令应该是php-r“echo s_func('somestring');”,引号是错误的。你不需要php标签来联系php程序员吗?
php -r "echo s_func("somestring");"