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:
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
download the php 5.2.17 source code
tar -zxvf php.5.2.17.tar.gz
cd php.5.2.17
./configure ./configure --prefix=/home/user1/php-5.2.17
make & make install
cd ext;
./ext_skel --extname=hello
cd hello
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])execute the command: /home/user1/php-5.2.17/bin/phpize
open php_hello.h, add
PHP_FUNCTION(hello_add);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); }./configure --with-php-config=/home/usr1/php-5.2.17/bin/php-configmake LDFLAGS=-lhelloThen the hello.so is generated under /home/usre/php-5.2.17/ext/hello/modules, but use
nm hello.soit prints:`U hello_add 0000000000000a98 T _init`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?
./configure ... --enable-hello; also, if you're wrapping a library you should use the--with-hello=DIRsyntax..m4you must rerunphpize.