简体   繁体   中英

Compiling C++ Programs with Emacs on Windows

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 'Mx 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))))))

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 Mx 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 "Mx 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.

The Mx compile command calls out to a shell (eg 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 Mx 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.

If you install Emacs and g++ in Cygwin, it should work just like Linux, which is to say that you can run Mx compile and use g++ myFile.cpp as your compile command. If you're using the "native" Emacs for Windows, or mixing Cygwin/MinGW/etc, things get a little more complicated (and other answers have solutions), but it boils down to making sure that your environment variables are setup correctly (eg PATH, SHELL, etc). Ultimately, if you can't run it on command line (either cmd.exe or /bin/bash from Cygwin or MinGW), it won't work in any Emacs.

As an example, I compile C++ in Emacs using both SCons/g++ from Cygwin and devenv.com from MS Visual Studio. This of course requires installation of SCons and Visual Studio, but it is possible. SCons installs from Cygwinports ( http://sourceware.org/cygwinports/ ) and ends up in /usr/bin, which is in the standard path, but devenv.com is buried deep in Visual Studio; to get access to it, I add it's containing directory to my PATH (in my ~/.bash_profile):

case $OS in
    Windows_NT)
        case "`uname -s`" in
            CYGWIN_NT*)
                PATH=${PATH}:"/cygdrive/c/Program Files (x86)/Microsoft Visual Studio 10.0/Common7/IDE/"
                ;;
            MINGW32_NT*)
                PATH=${PATH}:"/c/Program Files (x86)/Microsoft Visual Studio 10.0/Common7/IDE/"
                ;;
        esac
esac

I then construct a compile command of either scons or devenv.com -build "Release|Platform MySolution.sln , and things go merrily along from there. You can get as complicated as you need (in my current project I've got build and test shell scripts that callout to scons and devenv.com ; I tend to eschew batch files, as shell is much more powerful). My only complaint is that Microsoft changed the output of errors and so automatically jumping to lines in code from compiler output is currently broken (I need to look into fixing that).

I'm assuming that you are coming from the "native" build of Emacs, in which case, you might also find useful the cygwin-mount module for Emacs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM