Compiling the File

As mentioned at the beginning of this chapter, once we have our Lisp code in a file of its own, we can byte-compile it. Byte-compiling converts Emacs Lisp into a more compact, faster-running format. Like compilation in other programming languages, the result of byte-compilation is essentially unreadable to humans. Unlike other kinds of compilation, the result of byte-compilation is still portable among different hardware platforms and operating systems (but may not be portable to older versions of Emacs).

Byte-compiled Lisp code executes substantially faster than uncompiled Lisp code.

Byte-compiled Emacs Lisp files have names ending in .elc. As mentioned earlier, load and load-library, when given no file suffix, will preferentially load a .elc file over a .el file.

There are several ways to byte-compile files. The most straightforward ways are

From within Emacs: Execute M-x byte-compile-file RET file.el RET.

From the UNIX shell: Run emacs -batch -f batch-byte-compile file.el.

You can byte-compile an entire directory full of Lisp files with byte-recompile-directory.

When Emacs loads a .elc file, it compares the date of the file with the date of the corresponding .el file. If the .elc is out of date with respect to the .el, Emacs will still load it but it will issue a warning.

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.