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.
Related
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.
I'm trying to compile *.c file using shell_exec(gcc -o code.c code);
when I die dump I get that gcc is not recognized as a command but I do have it installed on my command. I can do shell_exec('ls') I also have the gcc path set I cain't think of anything else. can you? thanks ahead !
Please bear in mind at the moment I'm getting errors but gcc is actually compiling from a php function call in the web browser.
2>&1 means redirect stderr to stdout this link helped hope it can help you http://wiki.bash-hackers.org/howto/redirection_tutorial.
function compile()
{
$output = shell_exec("C:\MinGW\bin\gcc.exe -o $code 2>&1");
dd($output);
}
gcc.exe: fatal error: no input files\n
compilation terminated.\n
I can now use gcc with php shell_exec() function the errors I'm getting are now a simple understanding of the absolute paths my files have and making the correct gcc command to compile, the next step once I have actually managed to compile the code.c in .a.out will be to execute the .a.out file. I will share updates here once it is accomplished. Thanks for your help.
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.
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.
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.