I don't know how to compile and run a C++ file in GNU Emacs. I searched on the various sites and stackexchange network, but from them what I understood was write something like make compile, and I don't know what does that mean, obviously compiling something. I even have looked at GNU Emacs reference card and Dired reference card on gnu.org, but I could not find a helpful code and explanation of how to compile and run a C++ file.

Can someone help me with compiling and running, how each is done separately, and what steps are? For example, I see g++, gcc, -o, -Wall, foo.cpp, ./.a codes elsewhere but I don't know their function. Can someone explain their function, please? I have seen somebody's answer on stackoverflow which said:

if something is on your ~/PATH

What is that? How to put something in ~/PATH.

I know I'm asking for too much, but I don't want to use ready-go IDEs or compilers, I want to use GNU Emacs, and I want to learn it hard way. Thanks.

PS: I use Linux Ubuntu 16.somenumber as an OS. Thanks.

  • 4
    I'm voting to close this question as off-topic because this has very little to do with Emacs. Get a proper book on C++ that explains how to use a compiler, what the flags are for and why you'd want to write Makefiles. – wasamasa Jan 28 '18 at 22:28

That's a lot of questions, but I will try to answer them in such a way that you can look up all the answers in detail.

Emacs is primarily a text editor; you will use it to edit your C++ files. Converting those files into a program that your computer can run is the job of a compiler. There are many C++ compilers, but you will want to use one called gcc. Emacs can invoke gcc for you, but it's generally more useful to have Emacs invoke a program called make instead. make is useful because it can take instructions from a Makefile, which is a file you create that contains instructions for building complicated programs. make also has an interesting set of built in rules (called "implicit rules" in the documentation) which allow it to compile simple programs without a Makefile at all.

For example, let's say that you create a file called test.cpp, with this code in it:

#include <stdio.h>

int main(void) {
  printf("Hello World!");
  return 0;

Save this file, and then type M-x compile to tell Emacs that you want to compile this file. Emacs will prompt you for the command to run, and the default is simply make. Type in make test and hit enter, and Emacs will run make with the argumment test. make will then run and look for your test.cpp file, and use gcc to compile it into an executable named test. You can then run that executable at the command line like any other command-line program.

  • 3
    @garakchy And after having compiled the first time you can use recompile, so I suggest you to bind a shortcut to recompile – Nisba Jan 30 '18 at 11:40

I accept the answer above as the real one. However, here is my own 2 satoshis(cents :) after looking for the internet about the answer. I learned that there are mainly 2 compilers for C++ which are GCC and G++. GCC is for C and G++ is for C++ mainly as far as I understand. I don't know if they come with the Linux Ubuntu inherently installed but when I tried to install them it was already installed in my machine. To install them you need to install build-essentials package in Ubuntu. They are included in that package. After that I tried to compile my C++ program again. I wrote M-x compile and GNU Emacs showed me make -k which is a default compile argument in my GNU Emacs. I was hitting Enter button, writing something extra as g++ mycpp.cpp etc after make -k but to no avail. Finally, I decided to remove make -k and write only g++ mycpp.cpp and voile it worked. It put its output to the a.out in the same folder. To get read the output all I needed to was M-x shell or M-! and then ./a.out and it showed the output. I'm still open to the suggestions on how to edit default make -k or some other things so that instead of writing g++ mycpp.cpp I can just make -k it. I don't know, by the way, which one is a good practice for a programmer. I want to learn it. To the @wasamasa, look I learned something here but you voted my question to close. If it was closed I wouldn't have learned from suggestions here provided. I want to learn and improve myself. Thanks.

  • The reason I marked this question to be closed is because it's off-topic, meaning that this is the wrong place to learn about it. It would have been a different story if you asked about how to change the default command run by M-x compile. Feel free to open a new question for that specifically. – wasamasa Feb 1 '18 at 20:11

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