How to load accessory files in compiled code, Chicken Scheme

半城伤御伤魂 提交于 2019-12-07 13:30:49

问题


I'm currently working on a set of utilities, written in Chicken Scheme, and this is the first time I've tried writing a multi-file based program (or set of programs) in Chicken Scheme, and I'm having some trouble figuring out how to utilize code defined in accessory files correctly so that when you compile everything, the code defined in file A will be accessible to the compiled form of file B. I essentially need Chicken Scheme's equivalent to the following C code:

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

I've tried using all of the following, but they all produced sundry and unusual errors, errors like: '() being undefined, which doesn't make sense, since '() is just another way of writing (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

回答1:


I had to do some digging, but I finally figured out how to do this.

According to the wiki, if you have file bar.scm, which is relied upon file foo.scm, here's how you, essentially, #include bar.scm in 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)

Placing (declare (unit helper)) in helper.scm and (declare (uses helper)) in main.scm and compiling them thusly, worked:

csc -c main.scm -o main.o
csc -c helper.scm -o helper.o
csc -o foobar main.o helper.o


来源:https://stackoverflow.com/questions/22677321/how-to-load-accessory-files-in-compiled-code-chicken-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!