1

What I would like is to build a *.so , then make it as a PHP extenstion module and call the functions in the *.so via PHP.

My step is as below:

  1. Build the C library under linux, first Create the hello.c

    int hello_add(int a, int b)
    {
        return a+b;
    }
    

Then build as below:

    gcc -O -c -fPIC -o hello.o hello.c
    gcc -shared -o libhello.so hello.o
  1. download the php 5.2.17 source code

  2. tar -zxvf php.5.2.17.tar.gz

  3. cd php.5.2.17

  4. ./configure ./configure --prefix=/home/user1/php-5.2.17

  5. make & make install

  6. cd ext;

  7. ./ext_skel --extname=hello

  8. cd hello

  9. edit config.m4 by removing the dnl in line 16-18 then save and quit.

    16: PHP_ARG_ENABLE(hello, whether to enable hello support, 17: dnl Make sure that the comment is aligned: 18: [ --enable-hello Enable hello support])

  10. execute the command: /home/user1/php-5.2.17/bin/phpize

  11. open php_hello.h, add PHP_FUNCTION(hello_add);

  12. open hello.c change to:

    zend_function_entry hello_functions[] = { PHP_FE(confirm_hello_compiled, NULL) /* For testing, remove later. */ PHP_FE(hello_add, NULL) /* For testing, remove later. */ {NULL, NULL, NULL} /* Must be the last line in hello_functions[] */ };

    At the end of the file, add

    PHP_FUNCTION(hello_add) { long int a, b; long int result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) { return; } result = hello_add(a, b); RETURN_LONG(result); }

  13. ./configure --with-php-config=/home/usr1/php-5.2.17/bin/php-config

  14. make LDFLAGS=-lhello

  15. Then the hello.so is generated under /home/usre/php-5.2.17/ext/hello/modules, but use nm hello.so it prints:

                `U hello_add
                 0000000000000a98 T _init`
    
  16. Create a php file to test:

    <?php if(!dl('btest.so')) { echo "can not load hello.so"; } else { echo "load is done"; echo hello_add(3,4);// here it will throw error in the log } ?>

in the log, it complains: [28-Sep-2014 18:38:28] PHP Fatal error: Call to undefined function hello_add() ....

BTW, I copied the hello.so into another LAMP environment, not using the PHP just build. Both of the version is 5.2.17.

can anybody point what's going on?

6
  • You should do ./configure ... --enable-hello; also, if you're wrapping a library you should use the --with-hello=DIR syntax. Commented Sep 28, 2014 at 12:01
  • which step do you mean? step 5 or step 14 ? Commented Sep 28, 2014 at 12:06
  • At step 14, to configure your extension. Commented Sep 28, 2014 at 12:09
  • @Jack, I tried both by adding --enable-hello and --with-hello. Neither of them doesn't work. Commented Sep 28, 2014 at 12:33
  • When you change the .m4 you must rerun phpize. Commented Sep 28, 2014 at 12:34

2 Answers 2

1

While in step 15, change LDFLAGS=lhello to LDFLAGS=hello.o , then it works. I don't know what's wrong with the *.so. Anyway it's fixed now.

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

Comments

0

I tried --with-hello=DIR, what's the DIR? is the path of libhello.so or the php extension--hello 's path?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.