如何使用 lisp (clisp) 制作和使用库?
在 C/C++ 中,我可以创建一个库,并在源代码中使用 #include "" 使其成为静态库或 dll,并在链接时使用 -labc。 我如何在 Lisp 中拥有相同的功能?
以目录 A 中的 util.lisp 为例。我定义了一个库函数 hello。
(defpackage "UTIL" (:use "COMMON-LISP") (:nicknames "UT") (:export "HELLO")) (in-package util) (defun hello () (format t "hello, world"))
并尝试从主函数中使用该库函数。
(defun main () (ut:hello)) (main)
我尝试过
clisp main.lisp A/util.lisp
但是,我收到以下消息
*** - READ from #: there is no package with name "UT"
- 使用该库的 #include "" 相当于什么?
- 加载库的 -lutil 相当于什么? clisp/sbcl 使用该库的命令行是什么?
- 对于 defpackage,这相当于命名空间吗?
添加
我只需要加载库。
(load "./A/util.lisp") (defun main () (ut:hello)) (main)
运行“clisp main.lisp”效果很好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的称为系统。 Common Lisp 的 defpackage 与此无关,是的,它与命名空间有关。查看 HyperSpec 或 白痴指南(请参阅下面 Xach 的评论)以了解更多相关信息。
您可以将自己限制为仅加载文件,但通常会使用系统定义工具;现在主要是ASDF。一个最小的例子:
packages.lisp 包含包定义,stuff 和 more_stuff 是要加载的 lisp 或 fasl 文件。此系统定义(通常名为 filename.asd)必须符号链接到(或位于)
asdf:*central-registry*
中包含的目录,以便 ASDF 找到您的系统。然后,您可以这样加载系统:在最新版本的 ASDF 中添加了替代方案:
或者,在使用 slime 时,按
,l my-system RET
。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:
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:An alternative to this has been added in more recent versions of ASDF:
Or, when using slime, by pressing
,l my-system RET
.你必须在 main.lisp 之前加载 util.lisp:
Practical Common Lisp 有 关于定义和使用包的很好的介绍。
You have to load util.lisp before main.lisp:
Practical Common Lisp has a good introduction to defining and using packages.
Common Lisp 是一种基于图像的语言,尽管程度通常不如 Smalltalk。这意味着您可以通过使用 LOAD (如果明确使用,通常采用
(load (compile-file "your-file-here"))
) 形式,或者通常使用系统定义工具,如 ASDF。加载的代码可用于将来编译/加载的所有代码。包确实是命名空间。它们仅处理字符串到符号的映射,它们不直接连接到文件或函数或其他任何东西。您收到包错误,因为您尝试在定义包的文件之前使用包加载文件。
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.