Search code examples
emacselispdired

Byte compile emacs lisp file: Error: Cannot open load file


I am using Gnu Emacs 24.3 on Ubuntu 12.04. I downloaded dired+.el from here: http://www.emacswiki.org/emacs/dired%2b.el . I then tried to byte-compile this file in Emacs. I made a file bytecomp.el :

(byte-compile-file "dired+.el")

and run the following command:

bash$ emacs -batch -l bytecomp.el -kill

and got the following error message:

In toplevel form:
dired+.el:1114:1:Error: Cannot open load file: dired+

Update

Changing the file bytecomp.el to:

(add-to-list 'load-path "~/emacs/test/bytecompile")
(byte-compile-file "dired+.el")

and running emacs -batch -l bytecomp.el -kill from the same directory ( ~/emacs/test/bytecompile ) gives another error message:

Recursive load: "/home/fcihh/emacs/test/bytecompile/bytecomp.el", "/home/fcihh/emacs/test/bytecompile/bytecomp.el", "/home/fcihh/emacs/test/bytecompile/bytecomp.el", "/home/fcihh/emacs/test/bytecompile/bytecomp.el", "/home/fcihh/emacs/test/bytecompile/bytecomp.el"

Solution

  • You need to put dired+.el in your load-path.

    dired+.el explicitly does this:

    (provide 'dired+)
    (require 'dired+) ; Ensure loaded before compile this.
    

    This is an Emacs-Lisp idiom that ensures that the library is loaded before compiling it. For Dired+ this is appropriate.

    So do this:

    (add-to-list 'load-path "/your/path/to/dired+/")
    

    The relevant doc for the idiom used here is (elisp) Named Features. Here is a bit of it:

    Although top-level calls to `require' are evaluated during byte
    compilation, `provide' calls are not.  Therefore, you can ensure that a
    file of definitions is loaded before it is byte-compiled by including a
    `provide' followed by a `require' for the same feature, as in the
    following example.
    
      (provide 'my-feature)  ; Ignored by byte compiler,
                             ;   evaluated by `load'.
      (require 'my-feature)  ; Evaluated by byte compiler.
    
    The compiler ignores the `provide', then processes the `require' by
    loading the file in question.  Loading the file does execute the
    `provide' call, so the subsequent `require' call does nothing when the
    file is loaded.