Search code examples
schemechicken-scheme

Chicken scheme (use) and (import) in modules not working


I've experienced this problem in several projects, but this test case removes everything that is unecessary to understanding the problem. I've been using it to figure out what's wrong:

So I have a directory with 2 files in it, foo.scm, and bar.scm:

;;;foo.scm
(module foo (baz)
  (import chicken scheme)
  (define (baz)
    (display "bazzer!\n")))

;;;bar.scm
(module baz (quux)
  (import chicken scheme)
  (use foo)
  (define (quux)
    (baz)
    (display "quuxed!\n")))

baz.scm fails to run in csi with this error:

Error: (import) during expansion of (import ...) - cannot import from undefined module: foo

I've checked the docs: use should load the code into memory, and then import it. I've even run require, the function use uses to load code, separately. It runs without a hitch, it's just import that fails. I also tried to import the code in csi. use doesn't work, but require followed by import does, even though that should be equivalent to use.

In short, I'm hopelessly confused. Can anybody explain what's going on, and how to fix it?


Solution

  • (use foo) and (import foo) both will try to load a file called foo.import.so or foo.import.scm. This is a so-called "import library", which contains the necessary information to compile a module.

    This is comparable to a "header file" for C, and it's needed in order to make separate compilation possible. For example, in case of cross-compilation, it will be necessary to load the import library into the compiler on the build machine, but the library itself will be loaded into the program on the target machine.

    You can generate this import file using -J or -j foo when compiling foo using csc.

    The require procedure works at runtime, which means it's more like load than like use. I think you're confusing it with require-extension, which is like use.