- Friday, January 26, 2007
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:firstname.lastname@example.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
After checking out the sources,
ntdirectory. As a safety measure, issue a
cvs 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.
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 (
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.
- This package contains a variety of tools, and is
required in order to build Emacs. Specifically, it contains
- 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 GnuWin32
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 as
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, run
ntdirectory as follows to build a
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 for
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
--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:
cl.exeto your PATH in a normal Windows Command Prompt window. The easiest way to do this is to run the
vcvars32.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.c
We're also missing a few essential binaries the build process expects:
lib.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 its
bindirectory to your path after installing.
lib.exeis simply an alias for
link /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 --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
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
--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.
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" ..\bin