gpt4 book ai didi

common-lisp - 如何从 .asd 文件的目录编译和运行 Common Lisp 程序?

转载 作者:行者123 更新时间:2023-12-03 23:01:49 26 4
gpt4 key购买 nike

我有以下目录结构:

my-project/
├── my-project.asd
├── package.lisp # defpackage.
├── utils.lisp # Functions used by main.lisp.
└── main.lisp # Main program.
my-project.asd :
(defsystem "my-project"
:components ((:file "package")
(:file "utils")
(:file "main")))
package.lisp :
(defpackage :com.example
(:use :cl))
utils.lisp :
(in-package :com.example)

(defun double (x)
(* x 2))
main.lisp :
(in-package :com.example)

(format t "~a" (double 3))
问题是:如何编译和运行 main.lisp使用 ASDF?
我能够通过以下方式编译和运行该程序:
$ mv my-project ~/common-lisp/.
$ sbcl
* (require :asdf)
* (asdf:load-system :my-project)
然而,这是非常愚蠢的。我不想将我的项目移动到 ~/common-lisp/只是为了运行它。我想直接从项目目录本身编译和运行程序。 my-project/目录可以在任何地方,我希望它可以放在任何地方。换句话说,我想从当前目录加载系统。
想想 make ,我可以直接从 Makefile 的目录编译文件本身。如何从 *.asd 目录中类似地编译和运行 Common Lisp 程序文件本身?
(我使用的是 SBCL 1.4.5 版和 ASDF 3.3.1 版)

最佳答案

我发现可以执行以下操作:

$ sbcl
* (require "asdf")
* (asdf:load-asd "/absolute/path/to/my-project/my-project.asd")
* (asdf:load-system :my-project)
笔记:
  • (require "asdf")根据 ASDF manual 的“加载 ASDF”部分,推荐加载 ASDF 的方式。 .

    NB: all implementations except GNU CLISP also accept (require "ASDF"), (require 'asdf) and (require :asdf). For portability’s sake, you should use (require "asdf").


  • asdf:load-asd当给定的路径不正确(!)时不会因任何错误而失败,因此请确保给定的绝对路径是正确的。
  • 使用 cl:load而不是 asdf:load-asd似乎也可以工作,但 ASDF 手册明确警告不要这种做法:

    Indeed, ASDF does not load .asd files simply with cl:load, and neither should you. You should let ASDF find and load them when you operate on systems. If you somehow must load a .asd file, use the same function asdf:load-asd that ASDF uses. Among other things, it already binds the *package* to asdf-user. Recent versions of SLIME (2013-02 and later) know to do that when you C-c C-k when you use the slime-asdf contrib.


  • 关于common-lisp - 如何从 .asd 文件的目录编译和运行 Common Lisp 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65301060/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com