Unable to create PHP extension with swig - php

I am trying to generate a PHP extension of a C++ program (Simstring), but I am a little stucked.
The program already propose a python and a ruby extensions working with Swig.
I followed the Swig PHP documentation but I got an error.
Here is what I have done and the result I got, maybe you can help me with that:
cd swig;
mkdir php;
ln -s ../export.cpp
ln -s ../export.h
ln -s ../export.i
swig -c++ -php -o export_wrap.cpp export.i
g++ `php-config --includes` -fPIC -c export_wrap.cpp
g++ -shared export_wrap.o -o simstring.so
sudo mv simstring.so my-php-extension-dir/
I load the extension in my php.ini but I got the following error :
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20121212/simstring.so' - /usr/lib/php5/20121212/simstring.so: undefined symbol: _ZN6writer5closeEv in Unknown on line 0
Thanks a lot

Thanks to #zaufi, I searched more and found the answer.
I was definitively missing a file.
The right commands are the followings (please note the export_wrap.cpp AND export.cpp resp. export.o AND export_wrap.o)
g++ `php-config --includes` -fPIC -c export_wrap.cpp export.cpp
g++ -shared export.o export_wrap.o -o simstring.so
For more informations, you have to "include" the simstring.php generated file.

Related

Enable sqlite3 FTS5 for PHP

I'm trying to use SQLite3 FTS5 (full-text search) with PHP on CentOS.. but I couldn't get it successfully after so much time trying.
I'm building a loadable FTS5 Extension for SQlite and tried the following steps (from docs)
in the server terminal (SSH) :
$ yum install libsqlite3x-devel
$ wget -c http://www.sqlite.org/src/tarball/SQLite-trunk.tgz?uuid=trunk -O SQLite-trunk.tgz
$ tar -xzf SQLite-trunk.tgz
$ cd SQLite-trunk
$ ./configure && make fts5.c
$ gcc -g -fPIC -shared fts5.c -o fts5.so
But I'm having the following error in the last step:
fts5_index.c:732:11: error: ‘SQLITE_PREPARE_PERSISTENT’ undeclared (first use in this function)
fts5_index.c:732:37: error: ‘SQLITE_PREPARE_NO_VTAB’ undeclared (first use in this function)
fts5_main.c:888:29: error: ‘SQLITE_PREPARE_PERSISTENT’ undeclared (first use in this function)
fts5_main.c:1029:31: error: ‘SQLITE_PREPARE_PERSISTENT’ undeclared (first use in this function)
fts5_storage.c:139:15: error: ‘SQLITE_PREPARE_PERSISTENT’ undeclared (first use in this function)
fts5_storage.c:140:41: error: ‘SQLITE_PREPARE_NO_VTAB’ undeclared (first use in this function)
The file fts5.so is not produced so I can't use it in the server
So do you have any idea how to fix this error and produce fts5.so file successfully
I found that I have to generate header files before the last step
so the full steps looks like this:
$ yum install libsqlite3x-devel
$ wget -c http://www.sqlite.org/src/tarball/SQLite-trunk.tgz?uuid=trunk -O SQLite-trunk.tgz
$ tar -xzf SQLite-trunk.tgz
$ cd SQLite-trunk
$ ./configure
$ make fts5.c sqlite3.h sqlite3ext.h
$ gcc -g -fPIC -shared fts5.c -o fts5.so
Then I had to load the fts5.so as loadable extension for sqlite
1- Copy the fts5.so file to a new folder on server /sqlite_ext
$ mkdir /sqlite_ext
$ cp fts5.so /sqlite_ext
2- Edit sqlite3.extension_dir in php.ini to point to the same folder like this
sqlite3.extension_dir = "/sqlite_ext"
3- Then in my php file, load the extension :
$db->loadExtension('fts5.so');
Update :
It's better to update server's SQLite as a whole with --enable-fts5 option
$ wget -c https://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz
$ tar -xzf sqlite-autoconf-3280000.tar.gz
$ cd sqlite-autoconf-3280000
$ ./configure --enable-fts5 --prefix=/usr --disable-static CFLAGS="-g -O2 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_ENABLE_UNLOCK_NOTIFY=1 -DSQLITE_SECURE_DELETE=1 -DSQLITE_SOUNDEX"
$ make
$ make install
Now the latest SQLite is installed for the server but not for PHP, let's do it for PHP
$ mv aclocal.m4 config.m4
$ phpize
Check for your SQLite version in server using $ sqlite3 --version and in PHP using phpinfo();
*notice: the link mentioned in the first step is for the latest sqlite-autoconf amalgamation at the time of this answer. there maybe a more recent version for you . check here

Having problems while try to install OAUTH with PECL in MAMP on mac OS lion

i am new to setting php servers, and i had go though other related post, seems like nobody have the same error as i have.
I am using MAMP 2.0.2, and running PHP 5.3.6,
and I was trying to install oAuth on my local MAMP, using following commands:
$ cd /Applications/MAMP/bin/php/php5.3.6/bin
$ ./pecl install oauth
however, it return such error:
Notice: unserialize(): Error at offset 276 of 1133 bytes in Config.php on line 1050
ERROR: The default config file is not a valid config file or is corrupted.
What is happening? PECL is bundled in MAMP, which should be working out of the box....
Update:
I read a post elsewhere suggest that the config file's data, which holds the install paths are in-correct, so i changed some value in the following file :
/Applications/MAMP/bin/php/php5.3.6/conf/pear.conf
Then, i use the command:
$ ./pecl install oauth
Which it starts download and unpack, but when it try to install, it gives:
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
running: make
/bin/sh /private/tmp/pear/temp/pear-build-user1RU5EZA/oauth-1.2.2/libtool --mode=compile cc -I. -I/private/tmp/pear/temp/oauth -DPHP_ATOM_INC -I/private/tmp/pear/temp/pear-build-user1RU5EZA/oauth-1.2.2/include -I/private/tmp/pear/temp/pear-build-user1RU5EZA/oauth-1.2.2/main -I/private/tmp/pear/temp/oauth -I/Applications/MAMP/bin/php/php5.3.6/include/php -I/Applications/MAMP/bin/php/php5.3.6/include/php/main -I/Applications/MAMP/bin/php/php5.3.6/include/php/TSRM -I/Applications/MAMP/bin/php/php5.3.6/include/php/Zend -I/Applications/MAMP/bin/php/php5.3.6/include/php/ext -I/Applications/MAMP/bin/php/php5.3.6/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -Wall -g -c /private/tmp/pear/temp/oauth/oauth.c -o oauth.lo
mkdir .libs
cc -I. -I/private/tmp/pear/temp/oauth -DPHP_ATOM_INC -I/private/tmp/pear/temp/pear-build-user1RU5EZA/oauth-1.2.2/include -I/private/tmp/pear/temp/pear-build-user1RU5EZA/oauth-1.2.2/main -I/private/tmp/pear/temp/oauth -I/Applications/MAMP/bin/php/php5.3.6/include/php -I/Applications/MAMP/bin/php/php5.3.6/include/php/main -I/Applications/MAMP/bin/php/php5.3.6/include/php/TSRM -I/Applications/MAMP/bin/php/php5.3.6/include/php/Zend -I/Applications/MAMP/bin/php/php5.3.6/include/php/ext -I/Applications/MAMP/bin/php/php5.3.6/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -Wall -g -c /private/tmp/pear/temp/oauth/oauth.c -fno-common -DPIC -o .libs/oauth.o
In file included from /private/tmp/pear/temp/oauth/oauth.c:14:
/private/tmp/pear/temp/oauth/php_oauth.h:20:10:
fatal error: 'php.h' file not found
include "php.h"
^
1 error generated.
make: *** [oauth.lo] Error 1
ERROR: `make' failed
Again, what is happening?
I know this is old but I found this question while having a similar issue.
MAMP doesn’t ship with a bunch of the PHP sources
Download MAMP components and configure
URL: http://www.mamp.info/en/downloads/index.html (i.e. MAMP_components_2.0.2.zip)
Unpack your MAMP_components_2.0.2.zip
Identify your php-5.x.x.tar.gz file (where 5.x.x is your version of PHP)
If you are using php > 5.4.10 then download the sources from http://php.net/releases as they are not in the MAMP components download (credit pulkitsinghal in comments)
Create directory for your PHP sources:
mkdir -vp /Applications/MAMP/bin/php5/include
Untar php-5.x.x.tar.gz into /Applications/MAMP/bin/php/php5.*/include or /Applications/MAMP/bin/php5/ include:
tar zxvf php-5.x.x.tar.gz -C /Applications/MAMP/bin/php/php5.?.??/include
Rename your php-5.x.x directory to php (without the version numbering):
mv /Applications/MAMP/bin/php/php5.2.17/include/php-5.?.?? /Applications/
MAMP/bin/php/php5.2.17/include/php
Configure PHP sources (it’ll create necessary files i.e. zend_config.h, tsrm_config.h, etc.):
cd /Applications/MAMP/bin/php/php5.?.??/include/php
./configure
The process was for another fix but this resolved the issues with php.h not being found
Credit to where I found the answers - :
Thomas Hunter Blog
Google Doc detailing process
I had the same problem whilst trying to pecl install -f ssh2 with MAMP.
Here's how I fixed it:
MAMP doesn't provide the source code archive for PHP 5.4.10 so download it from php.net
Extract the source code archive to /Applications/MAMP/bin/php/php5.4.10/include/php
Run ./configure to configure the source code for your platform (without this step the pecl install will fail looking for a bunch of header files)
Retry your pecl install
(much thanks to Stephen's answer which is pretty much the same)
This is a complement to Stephen's answer and Greg's comment
When compiling xdebug 2.3.2 for php 5.6.2 on OSX 10.10.2, I could not get rid of the
'zend_config.h' file not found
error until I added the following option to ./configure in the php folder:
./configure --without-iconv
Credits to Cameron Browning
PECL Modules are compiled modules, in order to install them, you need the PHP headers. You can found the headers on php.net/downloads.php make sure you download a version which match with your PHP version. Then you can follow this : Installing PHP OAuth in MAMP environment

How to build WSO2 WSF/PHP 2.1.0 for FreeBSD 8.1?

I have a problem with building WSO2 WSF/PHP on my FreeBSD 8.1. It seems that something broken in the source.
I check all prerequisites then run ./configure && make and receive next error:
Making all in platforms/unix
/usr/local/bin/bash ../../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../.. -I../../../include -I../../../include/platforms -I../../../include/platforms/unix -g -O2 -D_LARGEFILE64_SOURCE -ansi -Wall -Wno-implicit-function-declaration -D_GNU_SOURCE -MT uuid_gen_unix.lo -MD -MP -MF .deps/uuid_gen_unix.Tpo -c -o uuid_gen_unix.lo uuid_gen_unix.c
mkdir .libs
gcc -DHAVE_CONFIG_H -I. -I../../.. -I../../../include -I../../../include/platforms -I../../../include/platforms/unix -g -O2 -D_LARGEFILE64_SOURCE -ansi -Wall -Wno-implicit-function-declaration -D_GNU_SOURCE -MT uuid_gen_unix.lo -MD -MP -MF .deps/uuid_gen_unix.Tpo -c uuid_gen_unix.c -fPIC -DPIC -o .libs/uuid_gen_unix.o
uuid_gen_unix.c: In function 'axutil_uuid_get_mac_addr':
uuid_gen_unix.c:332: error: 'SIOCGARP' undeclared (first use in this function)
After googling I found an advice to configure with ./configure 'CPPFLAGS=-DHAVE_GETIFADDRS'. After that make command almost finished but die at the end with following information:
gcc -g -O2 -D_LARGEFILE64_SOURCE -ansi -Wall -Wno-implicit-function-declaration -o .libs/test test.o ../../axiom/src/om/.libs/libaxis2_axiom.so ../../util/src/.libs/libaxutil.so ../src/.libs/libneethi.so /usr/home/alisachenko/src/wso2-wsf-php-src-2.1.0/wsf_c/axis2c/axiom/src/om/.libs/libaxis2_axiom.so /usr/home/alisachenko/src/wso2-wsf-php-src-2.1.0/wsf_c/axis2c/axiom/src/parser/guththila/.libs/libaxis2_parser.so /usr/home/alisachenko/src/wso2-wsf-php-src-2.1.0/wsf_c/axis2c/guththila/src/.libs/libguththila.so /usr/home/alisachenko/src/wso2-wsf-php-src-2.1.0/wsf_c/axis2c/util/src/.libs/libaxutil.so -lpthread -lz -Wl,--rpath -Wl,/usr/local/wsf_c/lib
../../util/src/.libs/libaxutil.so: undefined reference to `ftime'
What should I do to completely build WSO2 on my FreeBSD?
UPD1
Building after ./configure 'CPPFLAGS=-DHAVE_GETIFADDRS' 'LDFLAGS=-lcompat'
gcc -shared -Wl,--whole-archive ../src/platforms/unix/.libs/libaxis2_unix.a -Wl,--no-whole-archive -lcompat -lpthread -lz -Wl,-soname -Wl,libaxutil.so.4 -o .libs/libaxutil.so.4
/usr/bin/ld: /usr/lib/libcompat.a(ftime.o): relocation R_X86_64_32 can not be used when making a shared object; recompile with -fPIC
/usr/lib/libcompat.a: could not read symbols: Bad value
UPD2
Building after ./configure CPPFLAGS='-fPIC -DHAVE_GETIFADDRS' 'LDFLAGS=-lcompat'
gcc -shared .libs/hash.o .libs/allocator.o .libs/env.o .libs/error.o .libs/stream.o .libs/log.o .libs/string.o .libs/string_util.o .libs/qname.o .libs/array_list.o .libs/linked_list.o .libs/utils.o .libs/dir_handler.o .libs/file_handler.o .libs/class_loader.o .libs/network_handler.o .libs/file.o .libs/uuid_gen.o .libs/thread_pool.o .libs/property.o .libs/types.o .libs/param.o .libs/param_container.o .libs/dll_desc.o .libs/url.o .libs/stack.o .libs/generic_obj.o .libs/base64.o .libs/uri.o .libs/date_time.o .libs/base64_binary.o .libs/properties.o .libs/http_chunked_stream.o .libs/rand.o .libs/date_time_util.o .libs/version.o .libs/duration.o .libs/frame.o .libs/stomp.o .libs/md5.o .libs/digest_calc.o -Wl,--whole-archive ../src/platforms/unix/.libs/libaxis2_unix.a -Wl,--no-whole-archive -lcompat -lpthread -lz -Wl,-soname -Wl,libaxutil.so.4 -o .libs/libaxutil.so.4
/usr/bin/ld: /usr/lib/libcompat.a(ftime.o): relocation R_X86_64_32 can not be used when making a shared object; recompile with -fPIC
/usr/lib/libcompat.a: could not read symbols: Bad value
*** Error code 1
Stop in /usr/src/wso2-wsf-php-src-2.1.0/wsf_c/axis2c/util/src.
./configure 'CPPFLAGS=-DHAVE_GETIFADDRS LDFLAGS==-lcompat'
I am also trying to run WSO2 on various FreeBSD AMD64 without success.
I am compiling WSO2 in jail built on top of ezjail.
Since recompilation of libcompat is required I did it in "main" system. All the rest is done in the jail.
In main system (without jail) I recompiled libcompat (/usr/src/lib/libcompat) with -fPIC in makefile, so now the line looks like that:
CFLAGS+=-fPIC -DLIBC_SCCS -DSYSLIBC_SCCS -I${.CURDIR}/../libc/locale
[root#system /usr/src/lib/libcompat]# make install
install -C -o root -g wheel -m 444 libcompat.a /usr/lib
install -C -o root -g wheel -m 444 libcompat_p.a /usr/lib
Then
I copied libcompat.a and libcompat_p.a into basejail and switched to jail where I made a compilation
I found this: http://bsd.dischaos.com/2008/05/14/wso-web-services-framework-on-freebsd/
which inspired me.
I modified wsf_c/axis2c/util/src/platforms/unix/uuid_gen_unix.c and added
#include < sys/param.h>
(I put ^^^^ space here because I it was wrongly parsed by stackoverflow portal)
3 . Then I modified configure.m4 and I put the following path
PHP_ADD_INCLUDE(/usr/local/include/libxml2/libxml) replacing the original one
4 . Then still having problems with compilation I added these environment variables
export CPATH=/usr/local/include:/usr/local/include/libxml2:/usr/local/ssl/include/openssl::/usr/local/ssl/include
export LD_LIBRARY_PATH=/usr/local/lib
5 . And finally I managed to compile without errors
./configure "CPPFLAGS=-fPIC -DHAVE_GETIFADDRS" "LDFLAGS=-lcompat"
make
make install
My test jail on remote server doesnt have the web interface available yet and thus I couldn't test this installation on samples provided, but I am pretty sure that I would succeed since there were no errors during make nor during make install.
Php -m shows wsf module without any segfaults.
Samples are also working fine. I can't get wsdls yet, but it is rather matter of time.
The important problem you may face is openssl. It must be in ver 0.9.8 at least if I recall correctly. I am using 1.x. though
Although this setup is working fine on Freebsd 8.x I was unable to have it working on FreeBSD 6.x. On 6.x I finally built WSO2 successfully, but got segfaults when included wsf.so in php configuration ... then I gave up on 6.x and focused on freebsd 8.x where it seems to work fine ... just starting to do some programming with this module.

error in compiled in swig for connect c with php

swig -php example.i
This will produce 3 files example_wrap.c, php_example.h and example.php. The first file, example_wrap.c contains all of the C code needed to build a PHP extension. The second file, php_example.h contains the header information needed if you wish to statically link the extension into the php interpreter. The third file, example.php can be included by PHP scripts.
ya the three files are created, then execute the following coding.
this is used for create the example.so file.
gcc `php-config --includes` -fpic -c example_wrap.c
gcc -shared example_wrap.o -o example.so
but so many errors will displayed,
help me..
I think you're missing a step along the way, example.i should accompany example.[hc] so your compilation should go something like:
gcc -c example.c -o example.o
gcc `php-config --includes` -fpic -c example_wrap.c
gcc -shared example_wrap.o example.o -o example.so

Compiling geos without root CentOS

I have been trying to compile geos on my restrcited(no root) environment and I am having some difficulties...
I did the following
wget http://download.osgeo.org/geos/geos-3.4.2.tar.bz2
tar jxf geos-3.4.2.tar.bz2
cd geos-3.4.2
nano ~/.bash_profile
# I added PATH=$PATH:$HOME/local/bin export PATH
./configure --enable-php --prefix=$HOME/local/ && make clean && make
And Im getting the following errors
Making all in php
make[2]: Entering directory `/home/myname/test/geos-3.4.2/php'
Making all in .
make[3]: Entering directory `/home/myname/test/geos-3.4.2/php'
/bin/sh ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../include -I../include/geos `/usr/local/bin/php-config --includes` -DCOMPILE_DL_GEOS -I../capi -I../include -I./opt/alt/php53/usr/include/ -pedantic -Wall -ansi -Wno-long-long -ffloat-store -std=gnu99 -g -O2 -MT geos_la-geos.lo -MD -MP -MF .deps/geos_la-geos.Tpo -c -o geos_la-geos.lo `test -f 'geos.c' || echo './'`geos.c
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../include -I../include/geos -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DCOMPILE_DL_GEOS -I../capi -I../include -I./opt/alt/php53/usr/include/ -pedantic -Wall -ansi -Wno-long-long -ffloat-store -std=gnu99 -g -O2 -MT geos_la-geos.lo -MD -MP -MF .deps/geos_la-geos.Tpo -c geos.c -fPIC -DPIC -o .libs/geos_la-geos.o
geos.c:26:17: error: php.h: No such file or directory
geos.c:27:54: error: ext/standard/info.h: No such file or directory
geos.c:28:72: error: Zend/zend_exceptions.h: No such file or directory
According to some github, this happens with MAMP too
This happens because GEOS requires PHP header files from the original PHP source, and MAMP does not include those.
EDIT 1:
I have also added this in ~/local/share/config.site
CPPFLAGS=-I./opt/alt/php53/usr/include/
LDFLAGS=-L$HOME/local/lib
php.h is located here: ./opt/alt/php53/usr/include/php/main/php.h
info.h: ./opt/alt/php53/usr/include/php/ext/standard/info.h
zend_exceptions.h: ./opt/alt/php53/usr/include/php/Zend/zend_exceptions.h
EDIT 2:
Last thing: my ./configure tells me this
checking for php-config... /usr/local/bin/php-config
checking for php... /usr/local/bin/php
So my question, if Im on the right track for solving this, is how do I include my php header files while compiling geos in a non root evnironment in centOs ?
Im quite lost to be honest !
I'm fairly certain the path to your PHP installation does not start with a . - not from inside the geos-3.4.2 folder anyway, and certainly not from inside an arbitrary source folder.
You should make that path absolute.
I'm assuming the opt folder is in your user folder, so the full path would be $HOME/opt/alt/php53/usr/. Thanks for the clarification, the full path should then be /opt/alt/php53/usr/.
You need to add the bin folder to your PATH variable, the include folder to CPPFLAGS and the lib folder to LDFLAGS.
With $HOME/local/ this is not necessary, since that is the PREFIX, and will be added automatically.
Also note that when adding stuff to your PATH variable, you should add it to the beginning instead of the end, so that it will take precedence over system binaries.
I also suggest you undo the changes you made to ~/local/share/config.site (or remove the file, if it didn't exist), as options there will affect all automake-based projects.
You should rather pass CPPFLAGS and LDFLAGS as arguments to configure.
After all that, your command block should look something like:
wget http://download.osgeo.org/geos/geos-3.4.2.tar.bz2
tar jxf geos-3.4.2.tar.bz2
cd geos-3.4.2
export PATH="/opt/alt/php53/usr/bin:$PATH"
./configure --enable-php --prefix="$HOME/local" CPPFLAGS="-I/opt/alt/php53/usr/include" LDFLAGS="/opt/alt/php53/usr/lib" && make clean && make

Categories