eg sofile.c
===========================================
#include
void fun_in_so()
{
printf("This is function called from so");
}
int add_param(int x,int y)
{
return(x+y);
}
===========================================
STEP2 Create a file and list all C functions there and include C file as well
example.i
================================================
%module example
%include cpointer.i
%include typemaps.i
%include phppointers.i
%pointer_functions(int,intp)
#define PI 3.14159
#define VIKAS 123456
#define ANKIT "asdgasdgfasf"
%{
#include "sofile.c"
%}
extern void fun_in_so(void);
extern int add_param(int x,int y);
================================================
STEP3 Commands
swig -php example.i
[Above command will create three files example_wrap.c php_example.h, example.php ]
gcc `php-config --includes` -fpic -c example_wrap.c
[Above command will create example_wrap.o file]
gcc -shared example_wrap.o -o example.so
[Above command will create example.so file]
cp example.so /usr/lib/php/modules/
STEP4 Create a php file and call c functions
php sofile.php
================================================
dl("example.so");
echo PI;
echo "\n";
echo VIKAS;
echo "\n";
echo ANKIT;
echo "\n";
fun_in_so();
echo "\n";
$aa = add_param(2, 4);
echo "\nRes = $aa";
?>
OUTPUT
[root@vikas so]# php sofile.php
3.14159
123456
asdgasdgfasf
This is function called from so
Res = 6
[root@vikas so]#
=================================================
Hi Vikas, nice work here. But i am still struggling to run C code on my php page. Can you help me with that?
ReplyDeleteI am using windows xp os and gcc compiler
I made test.c and test.i files and run them using swig by using this command.
swig -php test.i
Three files were made test.php, test_wrap.c and php_test.h
But now i am struggling to create .so file. Can u please help me with that. I know i have to run this command.
gcc `php-config --includes` -fpic -c example_wrap.c
gcc -shared example_wrap.o -o example.so
But where shld i run it. I run it in cmd where swig is installed but it says cannot find php-config. Please help!!!