elisp warning “reference to free variable”

2019-06-24 08:51发布

问题:

I am wandering how to get rid of the elisp warning. my setup is the following:

I have init.el file which sets "emacs-root" variable:

;; root of all emacs-related stuff
(defvar emacs-root
   (if (or (eq system-type 'cygwin)
      (eq system-type 'gnu/linux)
      (eq system-type 'linux)
      (eq system-type 'darwin))
        "~/.emacs.d/"    "z:/.emacs.d/"
     "Path to where EMACS configuration root is."))

then in my init.el I have

;; load plugins with el-get
(require 'el-get-settings)

in el-get-settings.el I am loading packages with el-get and appending "el-get/el-get" folder to the load-path:

 ;; add el-get to the load path, and install it if it doesn't exist
 (add-to-list 'load-path (concat emacs-root "el-get/el-get"))

the problem is that I have a lips warning on 'emacs-root' in last expression for add-to-list : "reference to free variable 'emacs-root'"

what am I doing wrong here and is there any way to make the compiler happy?

this setup works ok btw - I don't have any issues during load time, just this annoying warning.

Regards, Roman

回答1:

When you are compiling the file where you reference the variable emacs-root, the variable must be already defined. The easiest way to avoid the warning is to add

(eval-when-compile (defvar emacs-root)) ; defined in ~/.init.el

in el-get-settings.el before the offending form.

Alternatively, you can move the defvar from init.el to el-get-settings.el.

Note that you can use eval-when-compile in defvar to speed-up loading the compiled file (of course, if you do that, you should not copy the compiled file between platforms):

(defvar emacs-root
  (eval-when-compile
    (if (or (eq system-type 'cygwin)
            (eq system-type 'gnu/linux)
            (eq system-type 'linux)
            (eq system-type 'darwin))
        "~/.emacs.d/"
        "z:/.emacs.d/"))
  "Path to where EMACS configuration root is.")

Note also that your original defvar emacs-root in the question if broken, it sets the variable emacs-root to "Path to where EMACS configuration root is." on windows.



标签: emacs elisp