[sword-devel] Compiled PHP SWIG Binding

Dean Montgomery dmonty at sd73.bc.ca
Mon Feb 6 13:51:55 MST 2006


After searching the mailing list archives and google I was unable to find how 
to create a Sword module for php.  So last Sunday, I debugged the 
swig-generated Sword.cxx so that it would compile as a php module.  

I was able to get swig to compile a Sword module for php.  I can see all the 
classes inside a php page.  I still need to get swig to properly handle the 
overloaded functions.  I hope to soon fix the swig overloaded functions 
issue.

Here is how you compile a Swig-Sword PHP module. Method 1 works, Method 2 does 
not yet work.

 cd bindings/swig/package/
 edit php4.m4 and add /usr/include/php4/main/ *OR* /usr/include/php/main to 
libraries.
fix autogen.sh "-ac" to "-a -c"
 ./autogen.sh
 ./configure
Make sure that the php4 installation and headers are detected.

*** METHOD 1 (works)
# edit bindings/swig/package/Makefile
# change phpswig to read this as follows:
$(SWIG) -php -c++ -o php/Sword.cxx -I$(top_srcdir) $(top_srcdir)/sword.i

make phpswig
# this is where you will see all the overloaded function warnings (this still 
needs to be fixed)

cd php

edit Sword.cxx and add "#define SWIG" to top This fixes the "pure virtual" 
errors.

change all the following in Sword.cxx:
ZVAL_STRINGL(return_value,result, 1, 1);
to:
ZVAL_STRINGL(return_value,(char*)result, 1, 1);

you can do the above using vim editor with substitute:
:%s/ZVAL_STRINGL(return_value,result/ZVAL_STRINGL(return_value,
(char*)result/gc


# Create the object
gcc -I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/include 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/main 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php 
-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM 
-I/usr/include/php/Zend -I/usr/local/include/sword -L /usr/local/lib -fPIC 
-shared -c Sword.cxx -g

# Create the shared library
gcc -shared -o Sword.so Sword.o /usr/local/lib/libsword.so

# Check if module is properly linked:
ldd Sword.so
        libsword-1.5.8.so => /usr/local/lib/libsword-1.5.8.so 
(0x00002aaaaac0b000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00002aaaaae30000)
        libc.so.6 => /lib/libc.so.6 (0x00002aaaaaf3d000)
        libcurl.so.3 => /usr/lib/libcurl.so.3 (0x00002aaaab174000)
        libidn.so.11 => /usr/lib/libidn.so.11 (0x00002aaaab2ab000)
        libssl.so.0.9.7 => /usr/lib/libssl.so.0.9.7 (0x00002aaaab3dc000)
        libcrypto.so.0.9.7 => /usr/lib/libcrypto.so.0.9.7 (0x00002aaaab513000)
        libdl.so.2 => /lib/libdl.so.2 (0x00002aaaab754000)
        libz.so.1 => /usr/lib/libz.so.1 (0x00002aaaab856000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00002aaaab96c000)
        libm.so.6 => /lib/libm.so.6 (0x00002aaaabb75000)
        /lib64/ld-linux-x86-64.so.2 (0x0000555555554000)

# cp Sword.so to php's modules directory
# adjust apache/php to load the Sword.so module
# restart apache, apache error log for errors.
# phpinfo(); should now have a line that says "Sword".
# The following php code lists all the Sword classes and methods.

<?php
echo "<pre>\n";
//print_r(get_loaded_extensions());  //shows "Sword".
$cls = get_declared_classes();
foreach ($cls as $key){
 echo "$key\n";
 print_r(get_class_methods($key));
 echo "\n\n";
}
echo "</pre>";
?>



/END METHOD 1

=========
*** METHOD 2 (does not yet work)
Attempt using `swig -phpfull` would not link to sword library, the following 
is just rough notes, and may be fixable. I recommend the above method 1.
=========

# edit bindings/swig/package/Makefile
# change phpswig to read this as follows:
$(SWIG) -php -phpfull -c++ -o php/Sword.cxx -I$(top_srcdir) 
$(top_srcdir)/sword.i


 make phpswig
 cd php
 # phpize it with phpize, phpize4, or phpize5 (SWIG website recommends php4) 
(debian/ubuntu uses /etc/alternatives)
 phpize
 grep -ris php5 *
 fix all of the above results of grep from php5 to php4
 ./configure --enable-Sword

edit Makefile and add "#define SWIG" This fixes the "pure virtual" errors.

change all the following:
ZVAL_STRINGL(return_value,result, 1, 1);
to:
ZVAL_STRINGL(return_value,(char*)result, 1, 1);



# Use lines below to compile and help locate errors (redirected to 2 and 1)
gcc -I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/include 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/main 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php 
-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM 
-I/usr/include/php/Zend -I/usr/local/include/sword -fPIC -c Sword.cxx -g -o 
Sword.lo  2>2 >1

make



gcc -I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/include 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/main 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php 
-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM 
-I/usr/include/php/Zend -I/usr/local/include/sword -c Sword.cxx -g -o 
Sword.la  2>2 >1

gcc -shared -c Sword.cxx -I/usr/include/php/main -I/usr/include/php/Zend 
-I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/local/include/sword 
-I/usr/include/php/regex -I/usr/include/php -o libsword_swig.so

-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/include 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php/main 
-I/home/dmonty/Download/sword-1.5.8/bindings/swig/package/php 
-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM 
-I/usr/include/php/Zend



# this might be fixable if we adjust libtool to do somethign like `gcc -shared 
-o Sword.so Sword.o /usr/local/lib/libsword.so` at the end.
the problem is the lining the shared library 
ldd Sword.so does not have a link to libsword.so

/END METHOD 2




More information about the sword-devel mailing list