0

I am developing an online testing system through which we can conduct online programming contests. For the same i want to invoke the C compiler from the php script using the functions like system(), shell_exec(), exec() etc. But the function is not creating a.out file when i wrote g++ a.cpp -o a using terminal it worked.

But this is not working

system("g++ a.cpp -o a",$as); //a.cpp is inside www folder

1 Answer 1

2

PHP runs as a different user to yourself, and so probably doesn't have permission to execute the g++ command.

To make the command accessible to PHP, try this:

sudo chmod o+x /usr/bin/g++

N.B. /usr/bin/g++ can sometimes be a symbolic link to another executable in the same directory, and so changing its permissions won't do anything (symlinks are 777 by default). If the above command doesn't resolve the issue, can you add the output of this command to your question, which will show where it points to:

ls -al /usr/bin/g++

On my system, this outputs

lrwxrwxrwx 1 root root      7 2011-08-10 14:52 /usr/bin/g++ -> g++-4.5

This means g++ is essentially an alias of g++-4.5, so I would allow 'other' execute permissions on g++-4.5 instead:

sudo chmod o+x /usr/bin/g++-4.5
Sign up to request clarification or add additional context in comments.

5 Comments

my issue is still not resolved output of command is " lrwxrwxrwx 1 root root 7 Mar 14 2012 /usr/bin/g++ -> g++-4.6 -rwxr-xr-x 1 root root 357312 Apr 16 2012 /usr/bin/g++-4.6"
In your case, run sudo chmod o+x /usr/bin/g++-4.6. I've added a more generic solution to my question.
i have tried that but it didnt work. p.s i had run the above code using terminal then refreshed the php file(index.php) and it didn't create a.out file if i have to do some other way then please mention as i am a newbie.thanks
Try it with absolute paths, e.g. system('g++ /var/www/a.cpp -o /tmp/a.out', $as);. If that works, you can then go on to change the output location - but make sure 'other' has write access to the directory.
Can whoever downvoted give a reason so I can improve my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.