I've been using Emacs for quite some time for basic text editing but as of today I am attempting to use it for c++ compilation. I have looked for the past few hours about how to go about this but I keep hitting roadblocks in their techniques (I think some of this is having to do with the tutorials being outdated).
Basically, all I want to do is be able to compile C++ programs that I write in Emacs through the 'M-x compile' command.
So far I have installed Cygwin and downloaded the packages for gcc. I have attempted some other solutions presented by tutorials online but they didn't pan out.
Thank you.
After installing a compiler. You can use the following snippet to customize compile command
(add-hook 'c++-mode-hook
  (lambda ()
    (unless (file-exists-p "Makefile")
      (set (make-local-variable 'compile-command)
       (let ((file (file-name-nondirectory buffer-file-name)))
         (concat "g++ -g -O2 -Wall -o " 
             (file-name-sans-extension file)
             " " file))))))
                        The M-x compile command calls out to a shell (e.g. linux bash, windows cmd.exe, etc) to run the make command. On windows I think emacs defaults to the cmd.exe shell (through a special C:/Program Files/Emacs/emacs/bin/cmdproxy.exe executable). 
If you want your M-x compile to use a different shell (probably cygwin bash in your case) then you need to tell emacs through changing shell-file-name variable or using the SHELL environment variable. You will also need to make sure that the cygwin make is found by changing exec-path variable (or using PATH environment variable).
To do this:
(setq shell-file-name "C:/cygwin/bin/bash.exe") 
(setq exec-path (cons "C:/cygwin/bin" exec-path))
And you could also look at setup-cygwin.el to set this up and some other things for cygwin.
You could try MinGW - "Minimalist GNU for Windows." I can't remember off the top of my head how to configure emacs to call custom build tools from M-x compile, but it should be possible. Here's a link I just came across that might be a starting point.
http://www.cae.tntech.edu/help/programming/free-programming-tools-windows
Write a Makefile for your program and use "make -k" for "M-x compile", as suggested by the Emacs documentation.  Using make (or similar build tools) is better practice than retyping command lines for GCC in general, not just for Emacs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With