2

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 May 6 '18 at 0:15
2

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
     program

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

1

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.

0

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.

  • 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 May 6 '18 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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