[英]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.