I have a c program
#include <stdio.h>
int main ()
{
printf("Hello");
}
On Windows:
I compile that program on windows and get a.exe
Now when I double-click on a.exe
Command windows opens
a.exe is run
and automatically close that windows.
To overcome this i have two solution
1> Create a batch file with the following content:
a.exe
pause
2> or add getch() function in my code
On LINUX
Now i want the same thing to happen in linux
I have compiled that program on linux get a.out and whenever i click on that nothing happens?
I have made one shell script:
#!/bin/bash
./a.out &
pause
and run that script by clicking on it but still nothing happened?
The thing you're not seeing is that it won't open in anything at all; the program will just execute quietly in the background.
To make the output visible, you'll need to make a shell script to run the program in a terminal, which can be as simple as this:
#!/bin/bash
gnome-terminal -x "`dirname \"$0\"`"/a.out
Mark the script as executable, and now you should be able to double-click it and see your program open in a terminal window. Note that tying the program to a particular terminal emulator (like gnome-terminal) is probably a bad idea, and you should rethink why you want this behaviour.
Linux doesn't distinct between terminal and GUI applications.
What you need to do is to actually configure the program to run in a terminal, or just run a terminal and execute the program in it.
Related
This question is quit similar to Vim [compile and] run shortcut
but what I want goes a little further. Is it possible to make a shortcut which compile and run the c code in the build in terminal and leave it open afterwards? The solution in the linked post just closes the output afterwards.
I guess the trick we used when coding Turbo Pascal and Turbo C++ would solve your problems. Just add a line for some dummy user input in the end of the program.
int main(void)
{
// Your code
getchar(); // Will not return to Vim before you have entered some data
}
You can combine !g++ % -o %< and :vert term ./%< together and make it a shortcut.
Here, ! allows to run external command from vim. g++ will compile the file, % indicates the current file, < is used to remove the file extension. :vert term is an internal command that lets you use terminal within vim.
Put the code in .vimrc file in home directory. The both commands combined would like,
map <F8> :w <CR> :!g++ % -o %< <CR>:vert term ./%<<CR>
When F8 button is pressed, vim saves the file then creates the object code. Afterward, with second command vim opens a terminal and runs the program. You will have to :q or type exit to close the terminal.
Make sure to exit insert mode before you hit F8.
I have a program that relies on a psuedo terminal that uses
term = posix_openpt()
grantpt(term)
unlockpt(term)
open(term)
to open a psuedo terminal. I'll call this program psuedoTerminal.bin
I want to open psuedoTerminal.bin from a bash script, in the following fashion (or similar)
#!/bin/bash
/bin/sh -c psuedoTerminal.bin &
The problem is when my program arrives to the posix_openpt() call, the behaviour is erratic, sometimes CPU consumption arrives to 100%, but it never works. I believe ssh, telnet, etc suffer from the same problems on opening psuedo terminals from inside scripts.
How can I run this program from inside a script? Thanks for your help.
I'm using Ubuntu 14.04.
There are 4 files involved: 'compile.sh', 'execute.sh', 'work.c', 'tester.sh'.
In 'compile.sh', it compiles the 'work.c' file and outputs an executable file called 'execute.sh'. In my own testing process, I do ./compile.sh, then ./execute.sh to run my C program. This works.
Now, the 'tester.sh' is a script that calls a Java program and this Java program does the same thing. It will run my 'compile.sh' first and then excute 'execute.sh'. It checks the correctness of my program outputs.
The problem is that when I do ./tester.sh, I get the error below
Reading first line from program...
./execute.sh: ./execute.sh: cannot execute binary file
First line of execution should match: Created \d heaps of sizes .+
Failed to execute (error executing ./execute.sh)
You can ignore the third line "First line of execution...."; it tries to check whether my output matches exactly with the tester. Since the binary file cannot be executed, then the first line does not match for sure.
So why does it say "cannot execute binary file"?
Content in compile.sh
#!/bin/bash
gcc -Wall work.c -o execute.sh
Content in tester.sh
#!/bin/bash
java -cp bin/tester.jar edu.ssu.cs153.work1.Tester
(bin/tester.jar is in my local machine; we can assume there is nothing wrong with the tester script.)
Diagnosis
It is weird, but not disallowed, to name an executable with the .sh extension. Your problem is that the Java code is trying to run it as a shell script (e.g. bash ./execute.sh), and it isn't a shell script so it fails. You need to change the Java to run the .sh file as an executable instead of as a shell script. Or, better (since you probably can't fix the Java), fix the compilation so that it produces an executable with a different name (e.g. work), and have execute.sh execute ./work.
File execute.sh is just an output file from compiling the work.c file. It is just like a.out by default from gcc. I can run ./execute.sh from the terminal and see all the correct outputs.
The trouble is, when you run it, you do ./execute.sh and the shell executes directly. The Java is running it as bash ./execute.sh, and that generates the error. Try it at the command line.
Prescription
On the face of it, you need to change compile.sh, perhaps like this (generating a program work from work.c):
#!/bin/bash
gcc -o work -Wall work.c
And you write a shell script called executable.sh that reads:
#!/bin/bash
exec ./work "$#"
This script runs your program with any command line arguments it is given. The exec means the shell replaces itself with your program; there are minor advantages to doing it that way, though it'll be OK if you omit the exec from the script.
I've been trying to open a new terminal window from my application and execute a command on this second window as specified by the user. I've built some debugging software and I would like to execute the user's program on a separate window so my debugging output doesn't get intermixed with the programs output.
I am using fork() and exec(). The command I am executing is gnome-terminal -e 'the program to be executed'.
I have 2 questions:
Calling gnome-terminal means the user has to be running a gnome graphical environment. Is there a more cross-platform command to use (I am only interested in Linux machines though)?
After the command finishes executing the second terminal also finishes executing and closes. Is there any way to pause it, or just let it continue normal operation by waiting for input?
You probably want something like xterm -hold.
1) gnome-terminal should work reasonably also without the whole gnome environonment, anyway the old plain "xterm" is enough.
2) you can execute a short bash script that launch your program and at the end reads a line:
bash -c 'my program ... ; read a'
(or also 'xterm -e ...')
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??
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]