简体   繁体   中英

execute terminal from c program

To run ac 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??

xterm -e "./test"

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

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.

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"

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM