【问题标题】:How to load accessory files in compiled code, Chicken Scheme如何在编译后的代码中加载附件文件,Chicken Scheme
【发布时间】:2014-03-27 02:47:19
【问题描述】:

我目前正在开发一组用 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;
}

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

;;; 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

【问题讨论】:

    标签: c compilation runtime-error chicken-scheme


    【解决方案1】:

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

    根据wiki,如果您有文件bar.scm,该文件依赖于文件foo.scm,那么您基本上就是#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)
    

    (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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      相关资源
      最近更新 更多