Custom PHP Extension Cross Compatibility - php

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.

Related

PHP extension library dependency

I have an extenal dependency for my PHP extension which I am struggling to compile successfully. I would like this extension to be a standalone shared object, which as I understand it is the job of phpize. How do I go about compiling this shared PHP extension with an object file c.o?
The manual compile for a standalone application would look as follows:
gcc -Wall -o php_library main.c c.o
and for a shared library:
gcc -Wall -shared -fPIC -o php_library.so main.c c.o
I understand that there are macros for adding in shared library dependencies by modifying config.m4 such as: PHP_ADD_LIBRARY(pthread,,EXTENSION_SHARED_LIBADD), but I'm not sure how to go about adding in my dependency at compile time. Is there a macro similar to the above for this use case?
It is worth noting that I do not have access to the depdency code, so c.o cannot be modified to solve this problem.
From what I understand from your question, you have an object file c.o which was not compiled to have position-independent code (i.e. -fPIC option) that you want to link into your PHP extension. In this case you must static link it alongside the extension code and any shared libraries. There are a couple ways to do this in the config.m4 file.
The first is to set CFLAGS to pass the name of the object code file to the linker like so:
CFLAGS=-Wl,c.o
The second is to modify LDFLAGS:
LDFLAGS=c.o
When I try the second one I get the following warning during compilation:
*** Warning: Linking the shared library testbed.la against the non-libtool
*** objects c.o is not portable!
Everything works either way so I'm not sure what's up with the warning. But the first method doesn't generate the warning.
Here's a complete sample config.m4:
PHP_ARG_ENABLE(testbed, whether to enable testbed support,
[ --enable-testbed Enable testbed support])
if test "$PHP_TESTBED" = "yes"; then
AC_DEFINE(HAVE_TESTBED, 1, [Whether you have testbed support])
PHP_NEW_EXTENSION(testbed, testbed.c, $ext_shared)
#LDFLAGS=c.o
CFLAGS=-Wl,c.o
fi
I hope this answers the question.

Issue setting up PHP on Virtuoso Server

I have a Virtuoso server running on Centos7 and have been trying to be able to execute PHP files from an HTML form (even really basic ones just to test), and have had no luck. I found out I had to install PHP and have been reading the documentation from the virtuoso GitHub README.php5 to setup PHP on the virtuoso server. This is my first time setting up PHP and I have run into an issue when trying to run the make command. I am in directory /etc/php-5.2.10 and have been able to run the configure command with all the flags. The error from the make command I receive is:
/bin/sh /etc/php-5.2.10/libtool --silent --preserve-dup-deps --mode=compile
/etc/php-5.2.10/meta_ccld -I/usr/local/iODBC/include -Iext/odbc/ -I/etc/php-5.2.10/ext/odbc/
-DPHP_ATOM_INC -I/etc/php-5.2.10/include -I/etc/php-5.2.10/main -I/etc/php-5.2.10
-I/usr/local/iODBC/include -I/etc/php-5.2.10/ext/date/lib -I/usr/include/libxml2
-I/etc/php-5.2.10/ext/mbstring/oniguruma -I/etc/php-5.2.10/ext/mbstring/libmbfl
-I/etc/php-5.2.10/ext/mbstring/libmbfl/mbfl -I/etc/php-5.2.10/TSRM -I/etc/php-5.2.10/Zend
-D_REENTRANT -I/usr/include -g -O2 -pthread -DZTS
-c /etc/php-5.2.10/ext/odbc/php_odbc.c -o ext/odbc/php_odbc.lo
In file included from /etc/php-5.2.10/ext/odbc/php_odbc.c:37:0:
/etc/php-5.2.10/ext/odbc/php_odbc_includes.h:104:22: fatal error: iodbcext.h:
No such file or directory
#include <iodbcext.h>
^
compilation terminated.
make: *** [ext/odbc/php_odbc.lo] Error 1
I do not know if these packages are related but for extra information, I have the following packages installed as well:
libiodbc.x86_64 3.52.7-7.el7
libiodbc-devel.x86_64 3.52.7-7.el7
php-odbc.x86_64 5.4.16-36.el7_1
unixODBC.x86_64 2.3.1-10.el7
unixODBC-devel.x86_64 2.3.1-10.el7
I have run sudo find / -iname '*iodbcext.h*' to try and determine where that file may be located and the only result I get back is /usr/include/libiodbc/iodbcext.h. However, I am not sure if that is what make is looking for or if there is supposed to be another one within the php-5.2.10 directory that is used, and if that is the right one, what would I do with it?
Any assistance or advice in getting PHP setup on the Virtuoso server is greatly appreciated.
You have conflicting packages installed, for the iODBC and unixODBC driver managers. It is strongly advised that you choose one or the other. Given that you're using Virtuoso (from my employer, OpenLink Software), I'd advise that you settle on iODBC (also maintained and supported by OpenLink Software, and generally expected to be found by Virtuoso).
PHP doesn't include the iODBC SDK (libiodbc-devel.x86_64), which is where the header file iodbcext.h would be found. I should note that the current version of iODBC is 3.52.10, somewhat later than the package you've installed...
It's not clear what options you passed to configure to get the make script you're running. You may be able to get past the error reported above by editing the second occurrence of -I/usr/local/iODBC/include in the make script, changing it to -I/usr/include/libiodbc ... but this is something of a guess.

Compile PHP extension using SWIG on Windows

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.

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

Php script to compile c++ file and run the executable file with input file

I need a php script to compile C/C++ file.
After compiling and getting the executable of that file i need to run that file with a argument of input.txt file and then compare the result with output.txt file
How can i do this ?
Call a command-line C++ compiler using system, exec, or backticks. There is no C++ compiler written in PHP.
Is PHP required? If not consider GNU Make. It was especially developed to solve such problems as your. With this simple Makefile:
OUT = app
CFLAGS = -O2 -Wall
$(OUT): main.o
clean:
rm -rf $(OUT) main.o
You get ability to easy compile your program with different compiler flags and performing clean operations:
make # for compiling
make clean # for remove binaries
Make has a good manual. Also, there is similar tool from Microsoft called nmake.

Categories