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.
Related
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 am working with command prompt features with a simple program to generate a text file in C. Here is the program:
#include <stdio.h>
int main(void)
{
char buf[80];
fgets(buf, 30, stdin);
printf("the input was %s\n", buf);
return 0;
}
My programming book is wanting to show how to play with the command prompt to make text files from programs, and instructs typing the word 'redirect' followed by '>' then the name of the program name with '.txt'. as below:
redirect> programname.txt
Now this IS generating a file 'programname.txt' on the desktop, but it is empty. The book purports that recipe should allow me to enter a string (as the program is DESIGNED to do) and that this string will be inside a generated programname.txt file. Also, there is a warning in the command line: "not recognized as an internal or external command". I've had this schpill before, but the text file generation did WORK, in that it did generate the .txt file. What am I missing here, for this program to work as intended?
You seem to be confused by the fact that it is not your program, but the shell which creates the file programname.txt, before it even tries to run your program.
And after the first succeeded and created an empty file, the latter probably fails because there is no command redirect in your PATH or such a thing exists as a builtin in your shell, as has already been suggested.
The usual way to perform output redirection in a shell is to use the > filename, but not with redirect before it contrary to what you say. The thing that comes before the > is the command to be redirected.
So, let's say you compile your program and save it as foo in the current directory (e.g. cc -o foo myprogram.c). In that case, you can redirect its output by saying:
./foo > filename.txt
This might sound like a retarded question but I am just learning C and all the websites I looked at this showed you the command to do it (project < somefile.txt) but not where to do it. Does it go in my project somewhere or command prompt? And if it is command prompt how do I get to where I need to enter it?
It goes on your command prompt. You first need to cd into location where your compiled binaries are. That command basically says "run project and feed contents of somefile.txt into it"
If you're on Windows hit Win+R and run cmd. On UNIX you should find terminal app somewhere in your menu.
Write your C code to use the built in FILE pointers defined in stdio.h. Read from STDIN and write to STDOUT. When you run your command like this: project < somefile.txt, the shell will open somefile.txt as STDIN before your program runs.
I'm a student and this is my first exposure to bash scripting, besides messing with a simple Makefile for c.
#!/usr/bin/sh
gcc -g -std=c99 -Wall -c field.c
gcc -g -std=c99 -Wall -c testField.c
gcc -g field.o testField.o -o testField
#testField get 0xa 0 1 > PA1output.txt
#testField get 0xaa 0 3 >> PA1output.txt
is my script.I want to compile field.c and testField.c into the executable testField.
No matter if I leave the last 2 lines commented out or not, they linux terminal hangs and after 10 seconds of nothing happening I press ctrl+c to stop it. Ultimately I want to redirect output to PA1output.txt, then concatenate things on the end of the file, but I want to rewrite the file contents each time.
As far as I understand it, > rewrites the contents of the specified file, and >> concatenates onto the end.
This is not my homework, I want to automate testing of other homework I have. 'testField get 0xaa 0 3 are arguments into my c program.'
I tried Bash script hangs
but that didn't answer my question totally.
My script is called 'as' to make it easy to type.
Why does the terminal hang and how do I get the script to do what I described above?
Thanks.
Your system has another program called ‘as’ which is an assembler. You are likely running this rather than your script, and it hangs because the assembler is waiting for input from your terminal.
If you insist on keeping the name, you should run your script with a full or partial pathname (like ‘./as’) so that the correct program is run.
You will probably find that your script will not run without the ‘#’ at the beginning of your first line. However, another way to run your script is ‘sh ./as’ from the command line, which does not depend on having the #! line.
As Jeremy described, it's most likely a conflict of names.
If you are running your script from the command line (I really hope you are), you don't have to be afraid of giving your scripts (and all file names for that matter) longer, but more specific, names. Most (if not all) command line interfaces on linux have some form of tab-expansion. All you have to do is type enough of the name to make it unique, then press [Tab], and the shell should complete the name for you.
Here's a more thorough explanation for Bash.
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.