views:

114

answers:

3

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.

+3  A: 

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.

Vijay Mathew
+1  A: 

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.

Ramarren
+3  A: 

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)

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

danlei
The "idiot's guide" is based partly on inaccurate CLTL1 information. The HyperSpec's chapters on packages are pretty readable and come with the bonus that they aren't loaded with inaccurate snarky remarks against the package system.
Xach
Xach, It's been some time since I read the idiot's guide, and even if I don't remember many details about it, I do remember that it was one of the explanations that helped me very much in understanding the concept. I've added a link to the relevant HyperSpec section, and a pointer to your comment after the idiot's guide link, so that readers can have a look at the normative, accurate, and an informal source of information.
danlei
The ASDF link is a bit broken.
Ax
Ax: fixed, thanks.
danlei