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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

how does argument order matter here?
I added a link to a previous answer; it is the order of arguments to g++ that I was talking about.
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 ...
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.
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 ....
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.