Page 1 of 1

Require commands only work when compiled in separate buffer?

Posted: Fri Aug 26, 2011 5:24 pm
by nick
Hi there,

Summary: If I try to compile some code with the require commands at the top the compilation fails with "package not found". If I put the require commands in a separate buffer and compile them, then come back and compile the rest of the code everything works fine.

Detail:

I'm following Adam Peterson's "Retro Games" Tutorial":

http://www.adampetersen.se/articles/lispweb.htm

I put all the code in a buffer as I go along and compile it with Slime commands.

If I put:

Code: Select all

(require 'cl-who)
(require 'parenscript)
etc.
at the top of the buffer and try to compile it I get "package not found errors".

If I put the require statements in a separate buffer and compile it then go back and compile the buffer with the game code it works.

Anyone got any ideas why this might be? It's not a big problem for me in terms of following the tutorial but I'd like to understand what is going on.

Any help appreciated.

Thanks.

Nick

Re: Require commands only work when compiled in separate buffer?

Posted: Fri Aug 26, 2011 10:30 pm
by ramarren
First, you have put this topic in the Emacs Lisp subforum while obviously talking about Common Lisp. Emacs Lisp is an extension language for Emacs editor, and is separate from Common Lisp, even if Emacs is commonly used to edit the latter. I have moved the thread.

As to the REQUIRE issue, REQUIRE is a function, and so is only executed at runtime. But file compilation works at file level, that is, the entire file is compiled first. Common Lisp is image base and for code to be compiled using libraries those libraries have to loaded into the image first.

Typically you would use a facility like ASDF to load your dependencies first, since REQUIRE is not very well defined across implementations. That doesn't really matter for a tutorial, just note that REQUIRE forms have to be executed first before attempting to compile/load any code depending on them.

Re: Require commands only work when compiled in separate buffer?

Posted: Sat Aug 27, 2011 3:12 am
by Kompottkin
To clarify, the file compiler does compile each top-level form separately. If you want the compiler to evaluate the require forms during compilation, you can tell it to do so using eval-when:

Code: Select all

(eval-when (:compile-toplevel :load-toplevel :execute)
  (require 'cl-who)
  (require 'parenscript))
(Strictly speaking, the :execute flag is not necessary here, but it is a good idea to include it in general, since you may want to be able to execute the form using things like C-M-x.)