3

我目前正在研究一组用 Chicken Scheme 编写的实用程序,这是我第一次尝试在 Chicken Scheme 中编写基于多文件的程序(或一组程序),我遇到了一些麻烦弄清楚如何正确使用附件文件中定义的代码,以便在编译所有内容时,文件中定义的代码A将可以被编译后的文件形式访问B。我基本上需要 Chicken Scheme 的等效于以下 C 代码:

#include "my_helper_lib.h"
int
main(void)
{
  /* use definitions provided by my_helper_lib.h */
  return 0;
}

我已经尝试使用以下所有方法,但它们都产生了各种不寻常的错误,例如:'()未定义,这没有意义,因为'()只是另一种写作方式(list)

;;; using `use`
(use "helper.scm") ;; Error: (require) cannot load extension: helper.scm

;;; using modules
;; helper.scm
(module helper (foo)
   (import scheme)
   (define foo (and (display "foobar") (newline)))) 
;; main.scm
(import helper) ;; Error: module unresolved: helper

;;; using `load`
(load helper.scm) ;; Error: unbound variable: helper.scm

(load "helper.scm") ;; Error: unbound variable: use
;; note: helper.scm contained `(use scheme)` at this point

;; using `require`
(require 'helper.scm) ;; Error: (require) cannot load extension: helper.scm
4

1 回答 1

4

我不得不做一些挖掘,但我终于想出了如何做到这一点。

根据 wiki,如果您有 file bar.scm,它依赖于 file foo.scm,那么您基本上就是#include bar.scm这样foo.scm

;;; bar.scm

; The declaration marks this source file as the bar unit.  The names of the
; units and your files don't need to match.
(declare (unit bar))

(define (fac n)
(if (zero? n)
  1
  (* n (fac (- n 1))) ) )
;;; foo.scm

; The declaration marks this source file as dependant on the symbols provided
; by the bar unit:
(declare (uses bar))
(write (fac 10)) (newline)

如此放置(declare (unit helper))和编译它们,helper.scm工作(declare (uses helper))main.scm

csc -c main.scm -o main.o
csc -c helper.scm -o helper.o
csc -o foobar main.o helper.o
于 2014-03-27T02:54:04.803 回答