5

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 a pre-existing terminal command prompt? I assume I use the system() function but what header is that in and what is the actual command arg it takes?

1
  • The terminal you want probably has a command line option to tell it what executable to run. So you could create a small shell script to call terminal with your program as a command line option. Commented Oct 19, 2013 at 23:59

3 Answers 3

7

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.

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

1 Comment

I just tried that and it works perfectly! T'anks. That's exactly what I was looking for.
0

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.

1 Comment

I'm looking to open a gnome-terminal.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.