I am new to ubuntu and was exploring the terminal. I got stuck here. I have two c++ files x.cpp and y.cpp . I am running x from the first terminal. It has a line as follows:
system("gnome-terminal");
this opens a new terminal window. Next it goes like:
system("g++ y.cpp");
system("./a.out");
but this runs y in the same terminal window. I want y to run in the newly opened terminal window. Please help.
Each call to system()
runs a separate new process, as a child of the calling process. There is no relationship between the processes (except that they have the same parent). Each call to system does not run another command in the same context as the previous call, like running on a shell command-line.
You can start gnome-terminal
with a command to run (instead of a shell prompt) so you can use system()
to start a gnome-terminal that runs the commands you want:
system("gnome-terminal -e 'sh -c \"g++ y.cpp && ./a.out\"'");
This will run the command gnome-terminal -e 'sh -c "g++ y.cpp && ./a.out"'
(but you need to escape the double-quote characters to put the command inside a C++ string literal).
That tells gnome-terminal
to run a shell (sh
) with the command g++ y.cpp && ./a.out