Compile PHP extension using SWIG on Windows - php

I'd like to create a PHP wrapper for an existing C++ library. I found a tutorial online that uses gcc but I'd like to do the same thing using the Visual Studio command line on windows.
I started doing this:
swig -php example.i
Which worked well. Then I have to build the created wrappers as an extension. The SWIG tutorial suggests the following:
gcc `php-config --includes` -fpic -c example_wrap.c
gcc -shared example_wrap.o -o example.so
What is the VS equivalent to those two calls? I'd use cl and link instead of gcc obviously but what to put instead of 'php-config --includes'? The VS command prompt does not recognize the php-config command as well as the '--'.
Thanks for you help.

The section of that command enclosed in backticks isn't a gcc argument, per se. The expression in the backticks is evaluated by the shell, and then that expression (including the backticks) is replaced by its result before actually being executed.
In this case, php-config --includes gives the arguments gcc needs to include all the appropriate php-related headers. On my install, the result is:
I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
That won't necessarily be valid for your computer. Just run php-config --includes to get what you what you need, take that output, and put it in the place of the 'php-config --includes' in the example you found.

Related

Custom PHP Extension Cross Compatibility

I am building my first PHP Extension using https://www.zend.com/resources/writing-php-extensions as a roadmap and running into some interesting (to me) issues.
Installed on the server is PHP 7.2, but the current master version is 7.4. I copied the php-src-master onto the server and do the first step of php ext_skel.php --ext test --dir and then
# phpize
# ./configure
# make
I then get this error:
/bin/bash /root/php-src-master/ext/test/libtool --mode=compile cc -I. -I/root/php-src-master/ext/test -DPHP_ATOM_INC -I/root/php-src-master/ext/test/include -I/root/php-src-master/ext/test/main -I/root/php-src-master/ext/test -I/usr/include/php/20170718 -I/usr/include/php/20170718/main -I/usr/include/php/20170718/TSRM -I/usr/include/php/20170718/Zend -I/usr/include/php/20170718/ext -I/usr/include/php/20170718/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/php-src-master/ext/test/test.c -o test.lo
libtool: compile: cc -I. -I/root/php-src-master/ext/test -DPHP_ATOM_INC -I/root/php-src-master/ext/test/include -I/root/php-src-master/ext/test/main -I/root/php-src-master/ext/test -I/usr/include/php/20170718 -I/usr/include/php/20170718/main -I/usr/include/php/20170718/TSRM -I/usr/include/php/20170718/Zend -I/usr/include/php/20170718/ext -I/usr/include/php/20170718/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/php-src-master/ext/test/test.c -fPIC -DPIC -o .libs/test.o
In file included from /root/php-src-master/ext/test/test.c:10:0:
/root/php-src-master/ext/test/test_arginfo.h:8:2: warning: implicit declaration of function ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE [-Wimplicit-function-declaration]
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, str, IS_STRING, 0, "\"\"")
If I download the src for 7.2, then everything works just fine. I guess I am surprised because I haven't edited any code at all at this point, so I would think this is the equivalent of a Hello World system, which seems like it would have ultimate compatibility between versions and so wouldn't throw an error unless I was calling functions that didn't exist in the installed version. If that is true and a "new" function is being called that wasn't in 7.2, it seems weird that a basic skeleton would be utilizing functions that didn't exist in 7.2 to display "Hello World".
If I were to create and compile in 7.2, would I later be able to compile in 7.4? If I compile in 7.4, will I be able to use it in 7.2?
The extensions I want to build will have very simple jobs, but ones that need to be done rapidly and frequently. Things like formatting phone numbers, filtering json, processing recursive tree relationships, and so on.
Having seen this compatibility issue, I am curious if this is going to lock me into using a specific version of PHP where even when I am not trying to use any new functions in my code, I still have to rewrite it every time we upgrade PHP in the coming years.
The skeletons for a specific PHP version branch may use functionality/internal APIs introduced in said version, if you want to build an extension compatible with a lower PHP version you should work from the PHP extension skeleton of said PHP version, not on from a more recent one.

Run C++ code with PHP

I have a some C++ code I would like to have run on a server and return the output. I know that
exec("./myprogram.exe", $out);
should run the program and $out will hold the output. Currently I can't get output from my program... it outputs it using cout and I compiled it on Windows, the server I want to run it on is Linux based. Any pointers?
First, you need to recompile your C++ code on Linux, using first g++ -Wall -g (order of arguments to g++ matters a lot) -then some other compiler arguments- since -Wall asks for all warnings and -g for debugging information. Once your code is debugged on Linux you could also pass -O2 to ask GCC to optimize.
Then you need to use the popen function of PHP to get the output of your command (thru a pipe). As documented, use e.g. fgets to read from the pipe handle, and don't forget to pclose it. See also this answer.

php-config shell script needed

I want to compile extension for PHP under Windows and I need to run such command:
g++ `php-config --includes` -fPIC -c some_script.cpp
but when I run this command I received an error:
g++: error: `php-config: No such file or directory
g++: error: unrecognized option '--includes`'
where can I get php-config shell script without recompiling PHP (under Windows)
The problem is not that you don't have the script, it's that the Windows shell doesn't know what you're trying to do because it doesn't support backticks. The command wants to run the php-config script and include the output in the command, but instead g++ is being passed \php-configas the first argument, and--includes`` as the second argument, which are not valid arguments to GCC.
You can't run a UNIX shell command in Windows. Either use a UNIX-style shell (e.g. via Cygwin or Mingw32) or just use a different command, e.g. figure out what the necessary include flags are and use them:
g++ -I/some/path -fPIC -c some_script.cpp

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

Categories