1

I am trying to make a .so PHP extension written in C using Zend framework. With stand-alone extension everything is OK. But if I try to use some other dynamic-linking library in my extension, I get the following error:

PHP Warning:  dl(): Unable to load dynamic library '/usr/lib/php5/20121212/test.so' - /usr/lib/php5/20121212/test.so: undefined symbol: _Z5hellov in /var/www/test.php on line 2
PHP Fatal error:  Call to undefined function my_function() in /var/www/test.php on line 3

I have compiled libhello.so and copied to the same directory /usr/lib/php5/20121212/ How can I use it from test.so module?

Here is the source code:

test.c:

#include "php.h"
#include "hello.h"

ZEND_FUNCTION( my_function );
ZEND_FUNCTION(Hello);

zend_function_entry firstmod_functions[] =
{
ZEND_FE(my_function, NULL)
    ZEND_FE(Hello, NULL)
    {NULL, NULL, NULL}
};

zend_module_entry firstmod_module_entry =
{
    STANDARD_MODULE_HEADER,
    "First Module",
    firstmod_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(firstmod)

ZEND_FUNCTION( my_function )
{
    char *str; int str_len;
    long l;
    if(ZEND_NUM_ARGS() != 2) WRONG_PARAM_COUNT; 

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", 
        &str, &str_len, &l) == FAILURE) {
        return;
    }

    zend_printf("%s  \r\n", str);
    zend_printf("%ld \r\n", l);

    RETURN_LONG(1);

}

ZEND_FUNCTION (Hello) {
    hello();
}

hello.h:

#include <stdio.h>

void hello();

hello.c:

#include "hello.h"

void hello () {
printf("%s\n", "Hello, world!");
}

test.php:

<?php
dl("test.so");
my_function("123",12);
    Hello();
?> 
0

1 Answer 1

2

In order to make a function visible to the php userland you need to use the macro PHP_FUNCTION():

hello.h:

PHP_FUNCTION(hello_world);

test.c:

ZEND_BEGIN_ARG_INFO_EX(arginfo_hello_world, 0, 0, 2)
    ZEND_ARG_INFO(0, arg1_name)
    ZEND_ARG_INFO(0, arg2_name)
ZEND_END_ARG_INFO()


...


const zend_function_entry pcap_functions[] = { 
    ...
    PHP_FE(my_function, arginfo_hello_world)
    ...
};


...
PHP_FUNCTION( my_function )
{
    char *str; int str_len;
    long l;
    if(ZEND_NUM_ARGS() != 2) WRONG_PARAM_COUNT; 

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", 
        &str, &str_len, &l) == FAILURE) {
        return;
    }

    zend_printf("%s  \r\n", str);
    zend_printf("%ld \r\n", l);

    RETURN_LONG(1);

}

...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.