- Friday, January 26, 2007
Emacs Hack #3: Compile Emacs from CVS on Windows
In previous hacks, we learned how to install and configure stable binary builds of Emacs. While the stable version (currently 21) is the best version to run for most users, you may be brave or curious enough to try one of the newest pre-release versions, 22 or 23. Because there are no official binary builds of Emacs beyond version 21, you will either need to install it from an unofficial source, or compile it yourself. This hack covers the latter.
Get the Source
The first step in compiling your own version of Emacs is getting the source. It is sometimes possible to obtain gzipped archives of the Emacs source at a given point, but it's typically more convenient to grab the source from CVS (this also ensures that you're using the most up-to-date version of the given branch).
If you don't already have CVS installed, fetch a recent version of
cvs.exefrom its distribution site, and place it somewhere in your path.Next up, we pay a visit to Savannah, the GNU development site. The Emacs project page contains details on how to obtain the sources. At the time of this writing, the following command will download the HEAD version of the code (Emacs 22).
cvs -z3 -d:pserver:anonymous@cvs.savannah.gnu.org:/sources/emacs co emacs
If you wish to build a version of Emacs other than 22, you will need to pass a tag name to your checkout command (or a subsequent update command). For example, adding
-r EMACS_21_3would check out the code for the Emacs 21.3 release (the stable release at the time of this writing). For a sneak preview of (the very unstable) Emacs 23, use-r emacs-unicode-2.After checking out the sources,
cdto thentdirectory. As a safety measure, issue acvs up -kbcommand here to make sure that all files in the directory have proper line endings. At this point, the source tree is ready to be built.Prerequisites
The Emacs build process requires a handful of tools that probably don't already exist on your system. These include GNU versions of tools with Windows equivalents (
cp,rm), as well as tools that are often unique to a GNU system (makeinfo). If you want your build to support images, you will also need a variety of libraries for rendering image formats. All of these tools are available from GnuWin32, a project which provides native Windows binaries of GNU tools.The packages you will need to install are listed below. In each case, I suggest installing the latest "setup" package, which will run an installer and place the binaries in a consistent location.
- CoreUtils
- This package contains a variety of tools, and is
required in order to build Emacs. Specifically, it contains
cpandrm. - TexInfo
- This package contains
makeinfo, which generates Info documentation from the texinfo sources in CVS. While technically optional, I strongly recommend installing it.
If you wish to build Emacs with image support (optional), you will also need the following image libraries (from the GnuWin32 Packages page:
At the time of this writing, the Xpm library is missing a required header file (
simx.h). You can either get it from the source package, or download it from the attachments in this post. This file should be placed in the GnuWin32includedirectory.Compiling with MinGW (GCC)
The simplest way to build Emacs is using MinGW, a collection of freely available tools for building native Windows binaries. The package includes the GNU C compiler, a port of
make, and various header files (including those from the Win32 API, such aswindows.h).The first step, of course, is to obtain the MinGW distribution if you don't already have it installed. The current version at the time of this writing is 5.1.3, and is downloadable at SourceForge.net (via the project page). Install to the location of your choosing, being sure to select the following components:
- MinGW base tools
- MinGW Make
After installing MinGW, add its
bindirectory to your path using a normal Windows Command Prompt session (set PATH=%PATH%;C:\MinGW\bin). Next, runconfigure.batfrom thentdirectory as follows to build aMakefile:configure.bat --no-debug --with-gcc
If you installed the libraries for image support, you will also need to pass the appropriate include path to the configuration script. If you installed to a directory with a space (the default), use the DOS name of the directory as demonstrated below:
configure.bat --cflags -IC:\Progra~1\GnuWin32\include --no-debug --with-gcc
If all goes well, you'll get a message telling you to run
gmaketo build Emacs. We're not quite ready for that yet, however. Because we got our source from CVS, we need to perform a "bootstrap" build. This creates a bootstrap Emacs binary to build autoloads and byte compile the Elisp (.el) files in the distribution. Begin the build process as below (mingw32-makeis the MinGW name forgmake):mingw32-make bootstrap
After the bootstrap completes, you're ready to build the source code. You'll also want to build the info files (make sure
makeinfo.exeis in your path) before installing. Run the following commands in sequence to finish compiling and install your Emacs build.mingw32-make info mingw32-make mingw32-make install
By default, Emacs will be installed "in place". If you prefer to install it somewhere else, run
configure.batwith a--prefix <dir>argument pointing to your preferred installation directory.Compiling with MSVC
The most popular C/C++ compiler for Windows is MSVC, Microsoft's Visual C++ compiler. Traditionally, this compiler was only available commercially, when purchased as part of Microsoft's Visual Studio development product. In recent years, however, Microsoft has made simple versions of the compiler available free of charge. Fortunately, the simple versions suffice for building Emacs.
At the time of this writing, the VC8 compiler (part of Visual Studio 2005) is the most recent version available. Unfortunately, Emacs cannot currently built with this version of the compiler without modifying the source. The only other freely available version of the MSVC compiler was made available as part of the Visual C++ Toolkit 2003. Microsoft no longer distributes this compiler (it was replaced by Visual C++ 2005 Express Edition), but if you already have it installed, or happen to have downloaded it previously (the file name is
VCToolkitSetup.exe), you can proceed to build Emacs using this compiler. If you have a commercial version of Visual Studio 2003 installed, the steps should be very similar.The Visual C++ Toolkit installation is very basic. In order to build Emacs, you need a variety of header files in addition to those packaged with the toolkit. To obtain these, you will need to download and install a (pre-Vista) version of Microsoft's Platform SDK. Because you only need a base set of components, the Web Install method is probably optimal. The following screen shot shows the components you need to have selected:

Next, add
cl.exeto your PATH in a normal Windows Command Prompt window. The easiest way to do this is to run thevcvars32.batscript packaged with the download, e.g.:"%ProgramFiles%\Microsoft Visual C++ Toolkit 2003\vcvars32.bat"
Next, you need to add the Platform SDK paths to your environment. For example:
set SDKROOT=%ProgramFiles%\Microsoft Platform SDK for Windows Server 2003 R2 set INCLUDE=%INCLUDE%;%SDKROOT%\Include set LIB=%LIB%;%SDKROOT%\Lib
If you are building with image support, you'll also need to add the GnuWin32 paths:
set INCLUDE=%INCLUDE%;%ProgramFiles%\GnuWin32\include set LIB=%LIB%;%ProgramFiles%\GnuWin32\lib
Normally we'd be done at this point, but we're still missing a couple of pieces. The first is
setargv.obj. Fortunately it's only missing in binary form -- it can be generated from the Platform SDK's CRT source directory. To build the required file and place it in the Platform SDK's lib directory, run the following command:cl /c /D_CRTBLD /I"%SDKROOT%\src\crt" /Fo"%SDKROOT%\lib\setargv.obj" \ "%SDKROOT%\src\crt\setargv.cWe're also missing a few essential binaries the build process expects:
nmake.exe,rc.exe, andlib.exe. All of these files are packaged with commercial versions of Visual Studio, but you'll need to find them elsewhere if you're using the Toolkit. The first two can be obtained free of charge by installing the .NET Framework SDK. Make sure you add itsbindirectory to your path after installing.lib.exeis simply an alias forlink /lib, so a simple batch file (lib.bat) will suffice. A sample is attached to this post -- place it somewhere in your PATH.Finally, we're ready to build Emacs using MSVC. As with the MinGW build, the process begins with running
configure.bat:configure.bat --no-debug --with-msvc
Things are pretty straighforward from here. As with the MinGW build steps, we need to start with a bootstrap build, after which we can build the complete distribution. Refer to the MinGW steps for details:
nmake bootstrap nmake info nmake nmake install
Optimized Builds
One of the advantages of compiling from source yourself is that you can optimize builds for your platform. For example, if you're using a modern AMD or Intel processor with SSE extensions, you can enable the compiler to generate optimized code for your processor. To build an optimized version of Emacs, you will need to pass arguments to the compiler by way of
configure.bat's--cflagsargument. To build a version of Emacs optimized for an SSE2-capable Athlon or Pentium 4 processor with MSVC, for example, you would run the script as follows:configure.bat --no-debug --with-msvc --cflags /O2 --cflags /G7 --cflags /arch:SSE2
For a similarly optimized build with MinGW (GCC), use:
configure.bat --no-debug --with-gcc --cflags -msse2 --cflags -O3
Optimized builds will typically be incompatible with older processors that do not support the selected extensions (in most cases, they will crash at runtime). If you are creating an Emacs build to share, minimize optimizations that require specific processor features such as SSE.
Final Steps
If you built Emacs with image support, you need to copy the runtime DLLs to Emacs'
binpath (or your system path, if compiling only for a single system). You can copy them manually, or script it as below (specific file names may vary slightly if versions change):for %f in (giflib4 jpeg62 libpng13 libtiff3 xpm4 zlib1) do \ echo copy "%ProgramFiles%\GnuWin32\bin\%f.dll" ..\binAttachments
Comments
- Monday, January 29, 2007 9:01:20 PM by HS
- Monday, March 05, 2007 7:50:24 PM by MatsThank you for a really nice and easy instruction.
It has always been suprisingly hard to understand what to install before the configuration, but this text describes it easily enough.
Instead of being very general, it specifically explains what to install to make it work for two cases:
mingw and msvc. I tried them both since this computer has msvc installed. I prefer the gcc solution though since that is completely free.
It would be nice if this text could be part of - Monday, March 05, 2007 7:52:36 PM by Mats".. the INSTALL file in emacs' nt catalog."
There _is_ a link.
I did mean to remove the entire text, not just half of it. :) - Tuesday, March 20, 2007 8:59:08 PM by CameronEasy Mode install for windows: http://ourcomments.org/Emacs/EmacsW32.html
- Saturday, May 26, 2007 2:50:00 PM by David RodSorry but I can't get it to work. My MingW won't load configure.bat from the NT directory. yes I have binaries in WinXP path. So I try with cygwin instead. Big mistake. Can't do make bootstrap and configure in directory above nt fails after 5 minutes at version.el
Can you provide instructions by cygwin users??? - Saturday, June 02, 2007 8:29:34 PM by DerekDavid, what sort of errors do you get when running configure.bat?
Also, just today Emacs 22 was officially released in binary form. See Hack #1 for details on installing binary versions. - Tuesday, June 05, 2007 11:48:51 AM by Michael KazarianFor possible exclude problems I do it:
1) download dos2unix from http://www.bastet.com/uddu.zip
2) emacs-cvs\nt> dos2unix makefile - Sunday, August 26, 2007 3:52:09 PM by nsbNice guide!
When inserting paths into the --cflags option I'd suggest using unix style '/' path seperators, as the CFLAGS are echoed into the config.h file. Using Windows style '\' seperators can be mis-interpreted by the compiler as escape codes and cause errors during compilation if they're not valid (The paths are still valid in Windows) - Tuesday, November 20, 2007 5:23:16 AM by JustinI finally took a bit of time out to walk through this and it worked fine for me with the latest MingW and coreutils.
Thanks! - Tuesday, November 20, 2007 5:58:10 PM by JustinA couple of things I had to change that weren't in the tutorial unless I did something wrong:
I had to add include and lib paths to point to where my image libraries were installed ...
-IC:\coreutils\include -LC:\coreutils\lib
And I found that adding
--cflags -mtune=pentium4
didn't work, so I adding the compiler options I wanted directly into the configure.bat file.
set usercflags=-mtune=pentium4 -O2 -msse3 -IC:\coreutils\include -LC:\coreutils\lib
Also I found that you cannot build emacs if you use a directory with spaces in the name. I had to move it to a directory with no spaces. - Wednesday, March 26, 2008 2:47:57 PM by David Mayfield.mingw32-make bootstrap FAILED
I followed the MinGW setup using cygwin bash prompt. The error was due to the --cflags setting given to configure.bat. ( don't add colon, space, or delmited space in the path )
To get it to work I moved the extracted GnuWin32 image libraries to C:/GnuWin32
And change the configure.bat options to:
configure.bat --cflags -I/GnuWin32/include --no-debug --with-gcc
Also when I ran:
mingw32-make install
I got the error:
rm: cannont remove 'c:/emacs32/emacs/nt/../same-dir.tst
mingw32-make: [install] Error 1 (ignored)
But everything had installed ok.
Thanks for the article Derek. I had followed two other tutorials but never managed it. - Saturday, October 04, 2008 4:18:36 PM by Petehi,
after installing via CVS my emacs folder is at 160 MB! how can i change this on windows (make clean doesnt work)? if i download the precompiled version of emacs it's only about 30-40 MB...
pete - Saturday, September 12, 2009 2:52:01 PM by RogerThank SO MUCH for putting these instructions on the web. Normally I would install using the pre-build windows binaries, but on my new job my boss says I can install anything I want but it has to be built from source, so now at least I can use EMACS again!
- Sunday, December 27, 2009 11:36:36 PM by Karl FogelGNU Emacs has switched from CVS to Bazaar now. See http://www.emacswiki.org/emacs/BzrForEmacsDevs for updated instructions on obtaining Emacs.
Don't know if you know, but there's an emacs binary on the net that includes basically your three hacks: it's a CVS version, it includes a server so you can open files from explorer in it, and it's of easy install on windows! Give it a try! (http://ourcomments.org/Emacs/EmacsW32.html)