views:

145

answers:

4

I have started using M-x compile to compile programs in say, C.

What is the most efficient way to run the executable e.g. a.out. At the moment I'm using M-! ./a.out. Basically, what is the best code-compile-run procedure to follow?

Almost all the tutorials mention how compilation is done, but I haven't seen one which addressed this issue?

Thanks, Samuel

+3  A: 

I often just append the run command at the end of the build:

M-x compile RET gmake && ./a.out

The && will tell the shell to only invoke a.out when the compilation is successful.

Though I often run the program from an existing shell buffer (M-x shell), or from one of many shells that I manage with package very much like screen.

Trey Jackson
I did something similar.
Samuel Chase
What should I do if the program requires user input? The compilation buffer is read-only. I don't want to have to open ansi-term or something.
Samuel Chase
@Samuel If there's user input, then I definitely use a *shell* buffer.
Trey Jackson
So there's no escaping it then... Thanks,Samuel
Samuel Chase
@Samuel One nice thing about running from a shell from w/in Emacs is that Emacs will keep track of what you've typed as input to the program (as opposed to a standard shell, which only remembers what you've typed into the shell itself). This allows you to re-use what you've already typed.
Trey Jackson
Which built-in is the best? I've tried eshell and ansi-term. Ansi-term makes the error messages of gcc look weird (accented characters, slashes etc), so I use eshell.Will have to figure out how to fix it later...
Samuel Chase
@Samuel I generally continue to use `M-x compile` for the compilation, and the shell for running. As far as which shell, I'm still stuck on tcsh, but any (bash, tcsh, csh, ksh) will work.
Trey Jackson
Sorry, I wasn't clear with the last question; what I meant was, how do you use bash (or any of the other shells) in emacs? Do you M-x shell, M-x eshell, M-x ansi-term M-x term or something else. I would like to know which one of these is the best.
Samuel Chase
@Samuel I use `M-x shell` as the answer says. :)
Trey Jackson
Thanks for the help.
Samuel Chase
jrockway
@jrockway I'll have to start giving eshell a chance.
Trey Jackson
+1  A: 

I use the extension smart-compile+, it allows you to specify quite convenient rules depending on the name of the file/buffer you are editing. For example, in my .emacs I specify the following rule for when running smart-compile in a project euler file:

(add-to-list 'smart-compile-alist
         '("euler.*\\.[Cc]+[Pp]*$" . "g++ -O2 -Wall -pedantic -Werror -Wreturn-type %f -lm -lgmpxx -lgmp -o ../bin/%n && time ../bin/%n")
         )

First you specify the filename pattern for the rule, then the path where it should be executed, then the compile command. In the above case, I add && time ../bin/%n to run the program directly after it has been successfully compiled.

Greget
I really have to learn what all those g++ options mean.
Samuel Chase
Ooh, smartness.
Ryan Thompson
A: 

My first choice is for interactive development; code, load files into the REPL, resolve errors (With C-x `, just as with M-x compile), and then play with the code in the REPL.

If that's not an option, I use eshell as a generic REPL. Compile, switch to eshell, run, test, repeat.

jrockway
A: 

Add a rule to your makefile to compile and run your program, so M-x compile will compile and run, test, valgrind-test... whatever

alcortes