Question

In C/C++, I can make a library, and make it static one or dll using #include "" in source code, and -labc when linking. How do I have the same feature in lisp?

As an example of util.lisp in directory A. I define a library function hello.

(defpackage "UTIL"
  (:use "COMMON-LISP")
  (:nicknames "UT")
  (:export "HELLO"))

(in-package util)
(defun hello ()
  (format t "hello, world"))

And try to use this library function from main function.

(defun main ()
  (ut:hello))
(main)

I tried

clisp main.lisp A/util.lisp 

But, I got the following message

*** - READ from #: there is no package with name "UT"
  • What's the equivalent of #include "" to use the library?
  • What's the equivalent of -lutil to load the library? What's the command line for clisp/sbcl to use the library?
  • And for defpackage, Is this equivalent to namespace?

ADDED

I just had to load the library.

(load "./A/util.lisp")

(defun main ()
  (ut:hello))

(main)

And run 'clisp main.lisp' works fine.

Was it helpful?

Solution

You have to load util.lisp before main.lisp:

> (load "util.lisp")
> (load "main.lisp")
> (main)
hello, world
NIL

Practical Common Lisp has a good introduction to defining and using packages.

OTHER TIPS

What you are looking for are called systems. Common Lisp's defpackage has nothing to do with this, and yes, it's about namespaces. Have a look at the HyperSpec, or the idiot's guide (see Xach's comment below) to read more about it.

You can restrict yourself to merely loading files, but usually, a system definition facility is used; mostly ASDF nowadays. A minimal example:

(defsystem my-system
  :name "my-system"
  :version "0.0.1"
  :author "myself"
  :license "LLGPL"
  :description "it's a system."
  :serial t
  :components ((:file "packages")
               (:file "stuff")
               (:file "more_stuff")))

Where packages.lisp would contain the package definition, stuff and more_stuff are the lisp or fasl files to be loaded. This system definition (usually named filename.asd) must be symlinked to (or located in) a directory contained in asdf:*central-registry* for ASDF to find your system. Then, you can load the system thusly:

(asdf:oos 'asdf:load-op 'my-system)

An alternative to this has been added in more recent versions of ASDF:

(asdf:load-system 'my-system)

Or, when using slime, by pressing ,l my-system RET.

Common Lisp is an image base language, although usually to a lesser extent than Smalltalk. This means that you use a library by loading it into the image, using LOAD (if used explicitly the often in form (load (compile-file "your-file-here"))), or usually with a system definition facility like ASDF. The loaded code is then available for all code compiled/loaded in the future.

Packages are indeed namespaces. They deal with mapping strings to symbols only, they are not connected directly to files or functions or anything else. You received a package error because you attempted to load a file using a package before a file defining it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top