Require commands only work when compiled in separate buffer?

Discussion of Common Lisp

Require commands only work when compiled in separate buffer?

Postby nick » Fri Aug 26, 2011 5:24 pm

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.


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

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)

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.


Posts: 1
Joined: Fri Aug 26, 2011 4:41 pm

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

Postby Ramarren » Fri Aug 26, 2011 10:30 pm

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.
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

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

Postby Kompottkin » Sat Aug 27, 2011 3:12 am

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.)
User avatar
Posts: 94
Joined: Mon Jul 21, 2008 7:26 am
Location: München, Germany

Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 1 guest