tags:

views:

1422

answers:

4

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.

+2  A: 

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

Andy White
By default Emacs will call make with M-x compile, and make can be installed with MinGW. If I remember correctly, the only problem is that make's executable actually isn't named make, you have to rename it.
Bastien Léonard
+3  A: 

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.

Nathan Kitchen
When executing the make command I keep getting an error saying " start-process-shell-command: Spawning child process: invalid argument ". I've tried using both 'make -k exercise' and just 'make exercise' both give the same error.
Jamin Huntley
Does make work if you type it in a console?
Bastien Léonard
It works when I type it in a normal cmd window but if I try to run make through emacs m-x shell command it doesn't recognize the make command.
Jamin Huntley
Try adding a line to your Makefile that sets the SHELL variable, such as "SHELL = /usr/bin/sh".Reference: https://lists.ccs.neu.edu/pipermail/prl/2008q2/002087.html
Nathan Kitchen
As long as you have the MingW32-g++ executable in your path, and you have the Makefile in the folder of the file you're compiling, then just M-x compile should work. Have you tried closing emacs after you changed the PATH variable and restart? You need to do that.
justinhj
+1  A: 

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))))))
Hamza Yerlikaya
+2  A: 

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.

luapyad
This works perfect for switching the default shell to bash but for some reason it isn't finding the make or gcc commands when I try to execute them.
Jamin Huntley
add cygwin's /usr/bin to you Windows PATH?
Arkadiy
Yeah, I have it added already.
Jamin Huntley