I recently moved from Code::Blocks to Emacs to write C code.

The one thing I don't like (because it takes too much time and effort for a simple and repetitive task) is the fact that I can't compile/build the code from within the software itself. So in order to compile, link and run my code I have to switch between Emacs and the terminal.

However, my professor said to me that I should be able to compile from Emacs itself by calling a buffer as a terminal, then compile and run the code like I would on my Linux terminal.

I've been searching on how to do this quite a bit now, but still couldn't figure it out. So I guess my question is: how can I compile and run my C code from within Emacs like I would with the terminal?

  • 2
    Does the Emacs manual, node Compilation help? C-h r followed by i compile takes you there.
    – Drew
    Commented May 6, 2018 at 0:15

3 Answers 3


I use the compile command, and also add a run target to my makefile like this

program: program.cpp
    gcc program.cpp -o program

run: program

So you can just use make run to compile and run it

  • 1
    Alternatively there is the quickrun package, which is handy for running programs directly with many other languages also Commented Jan 6, 2021 at 9:26

M-! let's you type a shell command and M-x shell takes you to a shell window. I'd probably set up makefile/CMake and run that.

Also if you install flycheck it should show you live if there are any errors in the file.


In order to simply compile a program you can use the built-in compile command.

To do this you need to have a makefile file in your current directory, then simply run M-x compile. A minibuffer will open where you can enter the name of the file you are trying to compile. Once you hit RET, a new buffer will pop open, reporting any errors or warnings while compiling, or a success message if none were found. It'll also point you to where in the source file the program failed to compile!

To read more about it, see the Emacs manual (C-h r), node Compilation.

  • 1
    If the compilation process is very simple (e.g. gcc myprogram.c -o myprogram) you can skip the makefile and write the command in the variable compile-command, set as a file local variable
    – matteol
    Commented May 6, 2018 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.