PHP extension library dependency - php

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.

Related

Having issues creating an php extension

I try to make a custom php extension for code Encryption purposes,
I followed this tutorial to make an custom extension: How to make a PHP extension
i get this warning when compiling the extension:
WARNING
The following arguments is invalid, and therefore ignored:
--enable-php-helloworld
i did all according to the tutorial, i build on windows.
what are possible things i do wrong.
I followed that section: https://stackoverflow.com/a/32575493/3103078
I replicated the code 1:1 (Os: Windows)
used commands:
phpize
configure --enable-php-helloworld
nmake
php -d extension=php_helloworld.so --re php_helloworld
Expected result:
>>>helloworld support
Actual Result:
>>>
reproduction:
mkdir php
start https://altushost-swe.dl.sourceforge.net/project/winflexbison/win_flex_bison3-latest.zip
set path=%path%;C:\your_path\to\bison
start https://altushost-swe.dl.sourceforge.net/project/gnuwin32/sed/4.2.1/sed-4.2.1-bin.zip
set path=%path%;C:\your_path\to\sed
start https://codeload.github.com/skvadrik/re2c/zip/refs/tags/2.1.1
set path=%path%;C:\your_path\to\re2c
start https://download.microsoft.com/download/5/C/3/5C3770A3-12B4-4DB4-BAE7-99C624EB32AD/windowssdk/winsdksetup.exe
rem after install
set path=%path%;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64
start https://windows.php.net/downloads/releases/php-devel-pack-7.3.29-Win32-VC15-x64.zip
rem install upper in php dir
phpize
configure --enable-php-helloworld
nmake
php -d extension=php_helloworld.so --re php_helloworld
The issue was not utilizing the php-src inside the sdk.
Also, on windows you need to use php-sdk-binary-tools, else it wont work when you compile it.
I'll do a tutorial on this soon.

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.

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.

How to compile PHP from source on MAC / OSX

Just for learning purposes, i tried to compile the current php source code from github, following the install instructions (see link).
I know that there are many other ways to achieve to install php like with homebrew and so on, but i am really interested in the way to compile it by myself just for education.
At first, i stumbled upon the fact, that there is no ./configure, even if the install document tells to execute this script.
So i googled around and found the following command to produce a ./configure - script:
$ autoreconf -i
aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
glibtoolize: putting auxiliary files in '.'.
glibtoolize: copying file './ltmain.sh'
glibtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.in,
glibtoolize: and rerunning glibtoolize and aclocal.
glibtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
No the file exists, but the execution of that script failed with a missing install-sh/install.sh/shtool:
$ ./configure
checking for grep that handles long lines and -e... /usr/local/bin/ggrep
checking for egrep... /usr/local/bin/ggrep -E
checking for a sed that does not truncate output... /usr/local/bin/sed
configure: error: cannot find install-sh, install.sh, or shtool in "." "./.." "./../.."
So i installed shtool via homebrew, but the same error still occurs.
Who can help me?
If you download the source from php.net, e.g.
http://php.net/downloads.php
It comes with a working configure

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