【问题标题】:emacs byte compile errors on the *first* require statement*first* require 语句上的 emacs 字节编译错误
【发布时间】:2014-04-27 14:42:55
【问题描述】:

我的 emacs init.el 文件有问题,这让我抓狂。

我的 init.el 将加载路径设置为一堆加载项,然后调用一些要求。

Emacs 加载正常,一切正常,但 flycheck for elisp 将 init.el 中的第一个 require 语句标记为“无法打开加载文件”,如果我进行字节编译,它也会给我同样的结果错误。

我创建了一个最简单的示例来重现问题,并在 MacOs X 和 Ubuntu 上使用 emacs 24.3 (9.0) 对其进行了测试。这两个测试都给了我同样的错误。

示例如下:

;; init.el --- entry point for configuration

(add-to-list 'load-path "~/.emacs.d/.")
(add-to-list 'load-path "~/.emacs.d/exec-path-from-shell")
(require 'test)
(require 'exec-path-from-shell)

;; test.el --- test lives in "~/.emacs.d"
(defvar test-var "this is test")
(provide 'test)

错误:

vagrant@elsa:~/.emacs.d$ pwd
/home/vagrant/.emacs.d
vagrant@elsa:~/.emacs.d$ sudo make
emacs --batch --eval "(byte-recompile-directory \".\" 0)"
Loading 00debian-vars...
Loading /etc/emacs/site-start.d/50autoconf.el (source)...
Checking /home/vagrant/.emacs.d...
Compiling /home/vagrant/.emacs.d/init.el...

In toplevel form:
init.el:5:1:Error: Cannot open load file: test
Checking /home/vagrant/.emacs.d/auto-save-list...
Checking /home/vagrant/.emacs.d/bin...
Checking /home/vagrant/.emacs.d/exec-path-from-shell...
Done (Total of 0 files compiled, 1 failed, 2 skipped in 2 directories)
vagrant@elsa:~/.emacs.d$

如果我改变 require 语句的顺序 ()

(require 'exec-path-from-shell)
(require 'test)

错误会变成:

In toplevel form:
init.el:5:1:Error: Cannot open load file: exec-path-from-shell

发生了什么事?我做错了什么?

问候,罗曼

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    字节编译器不会正常评估顶级表单。 require 是一种特殊情况,但扩展 load-pathadd-to-list 调用对字节编译器不可见。

    你需要将它们包裹在eval-and-compile:

    (eval-and-compile
      (add-to-list 'load-path "~/.emacs.d/.")
      (add-to-list 'load-path "~/.emacs.d/exec-path-from-shell"))
    

    这会强制字节编译器评估这些调用,因此load-path 在编译时被正确扩展。

    请注意,您不应将~/.emacs.d 添加到load-path,否则字节编译器会警告您。

    您可能应该只使用 package.el 来安装软件包。它会自动处理所有这些。

    【讨论】:

    • 非常感谢 - 这解决了问题。我实际上使用 el-get 来安装软件包,但我自己的库很少需要显式添加到加载路径。非常感谢!
    • C-h i g(elisp) Eval During CompileRET
    • 我有一个类似的问题困扰了我很久。当 Stackoverflow 为我找到这个问题时,我正要发布一个问题。太棒了。
    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 2015-01-21
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多