[英]How to load accessory files in compiled code, Chicken Scheme
我目前正在研究一套用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
我不得不做一些挖掘,但我终于想出了如何做到这一点。
根据维基 ,如果你有文件bar.scm
,这是依赖文件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)
在helper.scm
放置(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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.