【问题标题】:Byte compile emacs lisp file: Error: Cannot open load file字节编译emacs lisp文件:错误:无法打开加载文件
【发布时间】:2013-12-11 18:07:15
【问题描述】:

我在 Ubuntu 12.04 上使用 Gnu Emacs 24.3。 我从这里下载了dired+.elhttp://www.emacswiki.org/emacs/dired%2b.el。然后我尝试在 Emacs 中对这个文件进行字节编译。我做了一个文件bytecomp.el

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

并运行以下命令:

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

并收到以下错误消息:

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

更新

将文件bytecomp.el 更改为:

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

从同一目录 (~/emacs/test/bytecompile) 运行 emacs -batch -l bytecomp.el -kill 会给出另一条错误消息:

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"

【问题讨论】:

  • dired+.el 在目录~/emacs/test/bytecompile 中吗?如果不是,它的目录是否在您的load-path 中?
  • @Drew 是的,它在同一个目录中..

标签: emacs elisp dired


【解决方案1】:

您需要将dired+.el 放入您的load-path

dired+.el 明确地这样做:

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

这是一个 Emacs-Lisp 习惯用法,可确保在编译之前加载库。对于 Dired+,这是合适的。

这样做:

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

这里使用的成语的相关文档是(elisp)Named Features。这里有一点:

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.

【讨论】:

  • 谢谢,我做到了.. 但现在我在文件 bytecomp.el.. 上收到另一个关于“递归加载”的错误。
  • 不知道你为什么要让这个变得复杂,但尝试在-l bytecomp.el之前添加-l dired+.el。 (或者直接打开 Emacs,加载 dired+.el,然后加载 M-x byte-compile-file dired+.el。或者在 Dired 中使用 L,然后在 dired+.el 上使用 B。)关键是在编译之前需要加载 dired+.el .
  • 我想从bash 脚本自动编译字节。这就是我做复杂的原因:)
  • bytecomp.el 重命名您的文件。已经有一个具有该名称的标准 Emacs 库。这可能会让事情有点混乱。
  • 如果你只做emacs -batch -l dired+.el --eval (byte-compile-file "/your/path/to/dired+.el")会发生什么?
【解决方案2】:

编译dired+.el 不需要文件。直接从命令行调用字节编译器即可:

$ emacs -Q --batch -L . -f batch-byte-compile dired+.el

在调用此命令之前,删除您的 bytecomp.el 文件,或至少将其重命名为其他名称,例如my-byte-compilation.el.

您的 bytecomp.el 文件隐藏了内置的 Emacs 库 bytecomp.el,它定义了 byte-compile-file 和其他几个字节编译函数,包括上面的 batch-byte-compile

当调用byte-compile-file 时,Emacs 会尝试加载实现库bytecomp.el 以使函数定义可用。但是,由于您将文件命名为 bytecomp.el,并将包含目录放在 load-path 前面,Emacs 实际上会尝试再次加载 您的 文件。

这又会导致对byte-compile-file 的另一个调用,这又会触发bytecomp.el 的加载。因此,您看到了递归加载错误。

【讨论】:

  • 感谢您的信息。我会试试这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2011-09-14
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多