views:

20

answers:

1

I have various files of extensions .cpp stored on the server. When user clicks on the compile button, I should run gcc command passing the absolute path of the file to gcc compiler and then show the user the output of the file.
How can i do that???

+1  A: 

Kernel.system may the command that you are looking for: it calls a system process. For example:

if (system("gcc ..."))
  @output_to_show = %x['./compiled']
end

Assuming that your server is in Linux/UNIX, it would be good to separate the compile&execute process from the website server in order to reduce the security risks (by creating a new user just for this purpose). Or can you trust the content of the cpp-Files?

giraff
@giraff :-"You may want to run theses processes under another user-id, just to be sure they don't overwrite some of your hard-gained code files etc. ..."....I could not understand that. Can you alaborate a bit.
Silver Spoon
@giraff :- Thanks for the help.
Silver Spoon
@giraff :- Could you explain the %x['./compiled'] part in your code. How can i get the output/ errors in the compilation process
Silver Spoon
While system() only returns the success (or not) of the command, %x[] returns the outpout of stdout (see linked article: http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html).
giraff