'Linux C++, make application open terminal and run on it

I'm testing this on a basic Hello World C++ application. Since I can see the std output only if running the application from terminal, I would like to let the application open its terminal by itself (when started by double clicking it), then execute the rest of the code on it. I've already tried:

system("gnome-terminal");

just at the beginning of main() function. The terminal opens but nothing gets displayed. I guess it's because it is opened on another thread... ? So is there another way?



Solution 1:[1]

Two problems:

  1. You didn't create any method to communicate with the terminal. If you need bidirectional communication, you could create two pipes, one to go from your program to the terminal and one to go from the terminal to your program.

  2. The way you called system causes your program to wait for gnome-terminal to exit. You need to put an & on the end. In practice, you probably don't want to use system. You'll need to hook the pipes up in-between the fork and the exec, so you shouldn't use system in the first place. Then you can just not wait.

If you want to be ugly, you can use the /proc/self/fd/X mechanism. For example, in theory this could work:

system("gnome-terminal < /proc/self/fd/%d > /proc/self/fd/%d &",
    terminal_fd_in, terminal_fd_out);

Just make sure the descriptors are not set to close on exec.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Schwartz