If I have a C program that I compile successfully and generate an executable, how do I instruct the program to open a new terminal window when I run it from the command line in
The header file is stdlib.h
and the function signature is int system(const char *command)
. So in your case you could call the function like this to spawn a new terminal window:
#include <stdlib.h>
int main(void) {
int exit_status = system("gnome-terminal");
}
In C it's common to check the return value of most function calls to determine if something went wrong or to get some more information about the call. The system()
call returns the exit status of the command run, and is here stored in exit_status
for further inspection.
See man system
for the details.
You have to execute the terminal emulator. In my case (I have Kubuntu) it's Konsole, so it would be system("konsole")
.
If I wanted it to execute ls on the current working directory, it'd be:
system("konsole --hold -e ls .");
What you can't do with system is control the spawned terminal. On the other side, if you use fork+exec, maybe you can interact with it by redirecting its streams (dup2)
Depends on which terminal you want to open. There are several: xterm, konsole, gnome-terminal, and a whole bunch of others. For konsole, you would use:
system("konsole");
The terminal applications are usually in the default PATH, so you don't need to specify an absolute path.
As to which header provides system()
, all you need to do is read the manual page for it. You do that with the command:
man system
It provides a whole lot of documentation about system()
. Pay attention to the reasons why not to use system()
and whether they're important to you.