簡體   English   中英

如何在編譯代碼中加載附件文件,Chicken Scheme

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM