How to split project into multiple files

Discussion of Common Lisp
Post Reply
klkl

How to split project into multiple files

Post by klkl » Sun Aug 24, 2008 12:18 pm

I'm trying to write my first lisp program and I've got a problem how to split it into multiple files. I can't figure out how to make CL automatically compile and load a few files.

If I create file1.lisp with:

Code: Select all

(load "file2.lisp")
won't look for file2 in the same directory as file1.

I also tried defpackage, but packages aren't loaded automatcally either. Is there a simple way do do that? (writing ADSF files seems way too much for a hello-world program)

schoppenhauer
Posts: 99
Joined: Sat Jul 26, 2008 2:30 pm
Location: Germany
Contact:

Re: How to split project into multiple files

Post by schoppenhauer » Sun Aug 24, 2008 2:47 pm

I am not sure if I understand what exactly you want. If it gets on your nerves to always "load" all the files again, you might want to use Core-Images (under SBCL you can create these with sb-ext:save-lisp-and-die). If you always want to load some file on startup, you can put the load-command into the rc-file (~/.sbclrc, ~/.eclrc, etc., under linux, depending on the implementation you use). If you want a Package, maybe an .asd-File is the best. ASDF may be mighty, but for a simple "hello world"-application, you dont need to use all of it. The basic structure is simple:

Code: Select all

(defsystem "your-project-name"
  :description "Some Description"
  :version "..."
  :author "you"
  :license "quak"
  :depends-on (#:package1 #:package2) ;you probably wont need this
  :components ((:file "file1") (:file "file2") (:file "...")))
As most of these arguments are optional, it should be easy to use it. If this file is in the same directory as "file1", etc., then you should be able to load it using asdf.
Sorry for my bad english.
Visit my blog http://blog.uxul.de/

phil
Posts: 27
Joined: Wed Aug 13, 2008 1:23 pm
Contact:

Re: How to split project into multiple files

Post by phil » Sun Aug 24, 2008 2:54 pm

klkl wrote:If I create file1.lisp with:

Code: Select all

(load "file2.lisp")
won't look for file2 in the same directory as file1.
Well this should work:

Code: Select all

(let ((*load-pathname* *default-pathname-defaults*))
  (load "file2"))
but probably isn't recommended practice.
klkl wrote: I also tried defpackage, but packages aren't loaded automatcally either. Is there a simple way do do that? (writing ADSF files seems way too much for a hello-world program)
I agree that ASDF is a lot of work for a hello-world program but if it really is a simple program, you might consider just using one file. It would keep things a lot simpler in the short term while you play with the language.

WeYu
Posts: 9
Joined: Sun Aug 10, 2008 1:14 pm

Re: How to split project into multiple files

Post by WeYu » Thu Aug 28, 2008 4:05 am

klkl wrote:I'm trying to write my first lisp program and I've got a problem how to split it into multiple files. I can't figure out how to make CL automatically compile and load a few files.

If I create file1.lisp with:

Code: Select all

(load "file2.lisp")
won't look for file2 in the same directory as file1.
If you just want to keep it simple and don't want to mess around with asdf etc, you can just do like this:

Define the helper function load-relative:

Code: Select all

(defun load-relative (filename)
  (load (compile-file (format nil "~A~A" 
                                            (directory-namestring *load-truename*)
                                            (pathname-name filename))))
and then create a script "load-my-app.lisp" that contains:

Code: Select all

;; These can be placed into a separate file, like "load-deps.lisp"
(require :dep-1)
(require :dep-2)
(require :dep-3)

;; This can be put into a separate file too, like "packages.lisp"
(defpackage foo ... ) 

(load-relative "file1")
(load-relative "file2")
(load-relative "file3")
Some notes:
  • load-my-app.lisp should not be compiled, it's a script.
  • *load-truename* contains the full path of the file currently being loaded

marcoxa
Posts: 85
Joined: Thu Aug 14, 2008 6:31 pm

Re: How to split project into multiple files

Post by marcoxa » Tue Sep 02, 2008 8:24 am

WeYu wrote:
klkl wrote: ...
If you just want to keep it simple and don't want to mess around with asdf etc, you can just do like this:

Define the helper function load-relative:

Code: Select all

(defun load-relative (filename)
  (load (compile-file (format nil "~A~A" 
                                            (directory-namestring *load-truename*)
                                            (pathname-name filename))))
It is a good practice to create pathnames using MERGE-PATHNAMES and friends. Creating pathname strings and hoping they'd work is not advisable.

Cheers
--
Marco
Marco Antoniotti

humpolec
Posts: 14
Joined: Sun Aug 17, 2008 1:37 pm
Location: Poland

Re: How to split project into multiple files

Post by humpolec » Thu Sep 04, 2008 3:47 am

I have a question: ASDF lets you specify dependencies for your files, but is it a problem if my files have circular dependencies?

WeYu
Posts: 9
Joined: Sun Aug 10, 2008 1:14 pm

Re: How to split project into multiple files

Post by WeYu » Thu Sep 04, 2008 4:11 am

marcoxa wrote: It is a good practice to create pathnames using MERGE-PATHNAMES and friends. Creating pathname strings and hoping they'd work is not advisable.
Right, the load-relative function should probably look something like this:

Code: Select all

(defun load-relative (filename)
  (load (compile-file (merge-pathnames filename *load-truename*))))
I'm guessing that this should even work in cases like

Code: Select all

(load-relative "foo/bar/file4.lisp")

Post Reply