When you compile a file as a whole, it is first read as a whole. At that time, none of them has been rated yet, so the QT
package is not yet defined.
You can use eval-when
to evaluate something at an earlier time or use the system definition tool (ASDF is predominant today) to boot your system in the correct order.
eval-when
:
(eval-when (:compile-toplevel :load-toplevel :execute) (ql:quickload '#:qt)) (in-package #:qt)
Please note that you usually should not extinguish in library packages, but define your own fresh package to store your code:
(eval-when (:compile-toplevel :load-toplevel :execute) (ql:quickload '#:qt)) (defpackage #:qt-example (:use #:qt)) (in-package #:qt-example) ;; your code here
(If you're interested, defpackage
, defun
, defclass
, etc. are specially designed macros that expand to form inside eval-when
.)
This is sometimes normal for small one-time scripts. For systems of any remarkable size, especially if they have multiple source files, use ASDF:
;;;; qt-example.asd (in-package #:cl-user) (asdf:defsystem #:qt-experiments :description "Some experiments with QT." :serial t :components ((:file "package") (:file "qt-example")) :depends-on (#:qt)) ;;;; package.lisp (defpackage #:qt-example (:use #:qt)) ;;;; qt-example.lisp (in-package #:qt-example)
ASDF comes with most open-source Common Lisp implementations. You may need to configure the ASDF registry. I like to have one or two base directories for all my local projects, so I can just put the following in ~/.config/common-lisp/source-registry.conf
:
(:source-registry (:tree (:home "devel")) (:tree (:home "src")) :inherit-configuration)
ASDF then finds all the systems defined below these directories. In SLIME, you can simply use ,load-system
or ,open-system
from REPL with the system name to load it, respectively. open all the files in it, optionally download it.
When compiling one top-level form (using Cc Cc
) from a file, SLIME looks back from there for the in-package
form to find out which package it should accept. Usually you should have only one in-package
form for each file at the top.
A commonly used shortcut is Cc ~
in Lisp source files, which switches the REPL to the file directory and the effective package at a point.