Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C++ Programs with Emacs on Windows

Tags:

c++

emacs

gcc

ide

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.

like image 308
Jamin Huntley Avatar asked Apr 11 '09 21:04

Jamin Huntley


4 Answers

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))))))
like image 90
Hamza Yerlikaya Avatar answered Nov 12 '22 12:11

Hamza Yerlikaya


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.

like image 24
luapyad Avatar answered Nov 12 '22 13:11

luapyad


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

like image 3
Andy White Avatar answered Nov 12 '22 14:11

Andy White


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.

like image 3
Nathan Kitchen Avatar answered Nov 12 '22 13:11

Nathan Kitchen