0

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?

beenjaminnn
  • 752
  • 1
  • 11
  • 23

1 Answers1

1

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • how does argument order matter here? – Karoly Horvath Jan 25 '14 at 18:48
  • I added a link to a previous answer; it is the order of arguments to `g++` that I was talking about. – Basile Starynkevitch Jan 25 '14 at 18:50
  • 1
    It (order of program arguments to `g++`) usually is not irrelevant, especially if your compiled program uses some libraries. The OP did not tell us what is his C++ program and how big is it ... – Basile Starynkevitch Jan 25 '14 at 18:53
  • in case you have a selective hearing: you ignored the word **here**. "order of -Wall and -g is important" - that's the way I read your sentence. – Karoly Horvath Jan 25 '14 at 18:54
  • It is relevant *here* especially if the C++ program has several source files and use several libraries. You won't link it successfully with `g++ -lgmp foo.cc bar.cc -I/usr/local/include -L/usr/local/lib -o foobar`; and the "order matters" refers to put `-Wall -g` *first* .... – Basile Starynkevitch Jan 25 '14 at 18:56
  • @Karoly: Please recall that I am not a native English speaker. I'm sure you might have difficulties having all that chat in e.g. French or Russian! – Basile Starynkevitch Jan 25 '14 at 19:05
  • I have myprogram.out now, how can I read the output from `$handle = popen('./myprogram.out', 'r');`? – beenjaminnn Jan 25 '14 at 19:08