如何在编译代码中加载附件文件,Chicken Scheme [英] How to load accessory files in compiled code, Chicken Scheme

查看:330
本文介绍了如何在编译代码中加载附件文件,Chicken Scheme的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一系列以鸡档案编写的实用程序,这是我第一次尝试在鸡档案中编写一个基于多文件的程序(或一组程序),而我在正确地利用附件文件中定义的代码时遇到一些麻烦,所以当你编译所有的东西时,文件 A 中定义的代码将可以被编译的文件 B 。我基本上需要Chicken Scheme等同于以下C代码:

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

我试过使用以下所有内容,异常错误,错误如:'()未定义,这没有意义,因为'()只是另一种写(list)的方式。

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


推荐答案

我不得不做一些挖掘,但我终于找出了如何做这个。

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

根据 wiki ,如果您有文件 bar.scm ,这是依赖于文件 foo.scm ,这里是你,基本上, #include code>

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)

helper.scm 和<$中放置(declare(unit helper)) c $ c>(declare(uses helper)) main.scm 并编译它们,工作:

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

这篇关于如何在编译代码中加载附件文件,Chicken Scheme的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆