Search code examples
lispcommon-lispclisp

How to make and use library with lisp (clisp)?


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.


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.