Calling gcc with shell_exec in php

不羁的心 提交于 2019-12-14 02:52:44

问题


<?php
$output=shell_exec('gcc prog.c');
echo "$output";
?>

I'm trying execute a c program using php and have used shell_exec to call gcc to execute the program but it is giving no output but there is no error being showed . Can please someone correct the mistake Thank you in advance.


回答1:


gcc is used to compile the c file. It doesn't 'run' the .c file. Try it from your command line. You will notice after running gcc prog.c you have a file named 'a.out'. a.out is the executable that is created from the successful compile of prog.c




回答2:


Calling gcc will launch a compile of the prog.c file. It's not executing it.

If you need your prog.c file to be compiled at runtime, I'd write a quick shell script that would look somewhat like that:

#!/bin/bash

rm prog_compiled_from_php               # Remove previously compiled program
GCC=`which gcc`                         # Find the path to gcc and store it in the "GCC" variable
$GCC prog.c -o prog_compiled_from_php   # Compile prog.c into the binary called prog_compiled_from_php
./prog_compiled_from_php                # Execute the compiled program

Save this file as prog_compile

Make sure you make this script executable, with this: chmod a+x prog_compile

From PHP, call $output = shell_exec('prog_compile');

I'm no bash script expert, feel free to correct my syntax :)



来源:https://stackoverflow.com/questions/12806365/calling-gcc-with-shell-exec-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!