gvalero87
gvalero87

Reputation: 875

execute terminal from c program

To run a c program you do something like this

bash> gcc test.c -o test

and then

bash> ./test

How can i do to make test.c execute a terminal in another window??

Upvotes: 1

Views: 6281

Answers (5)

Terminal
Terminal

Reputation: 1999

You can fork a new process and use system() function. This will work on most of Linux distributions. Just check the terminal properties to know the command to execute a new terminal. "gnome-terminal" works for me(Ubuntu, Redhat).

int main()
{
        if(!fork())// child process
        system("gnome-terminal");
        else
        {
                //do rest of the things here in parent process......
        }

}

After fork(), a new terminal window will open as a separate process.

Upvotes: 2

Foo Bah
Foo Bah

Reputation: 26281

Based on your use of the word terminal im guessing you are using osx.

You can use applescript to get the behavior:

tell application "Terminal"
activate
do script with command "cd _directory_; ./test"
end tell

If you want the program to launch a window, have the program call popen to launch the command [or write to a temporary file and launch the script]

Upvotes: 0

initzero
initzero

Reputation: 852

xterm -e "./test"

This will execute 'test' in a new xterm window. Assuming Linux of course.

Upvotes: 2

Jaffa
Jaffa

Reputation: 12719

You want to open a window for a new terminal, or what do you want to do? Your question isn't really clear.

If you want to run some commands, you need to cope with sys calls to launch a new process.

On Windows there's the system() function, but I'm not sure it exists on Linux or other posix systems.

Upvotes: 0

aioobe
aioobe

Reputation: 421160

Depends on which system you're on and which terminal you have in mind, but here's how to do it if you're on gnome (ubuntu for instance)

gnome-terminal -x sh -c "./test"

If you don't want the window to close immediately after ./test finishes, you do

gnome-terminal -x sh -c "./test; cat"

Upvotes: 0

Related Questions