[previous] [up] [next]     [index]
Next: Autodetecting Compiled Files for Up: Compiling Individual Files with Previous: Compiling Individual Files with

Prefixing Compilation with Macro and Signature Definitions

A load or require-library expression in a source file is compiled--but not evaluated!--as the source file is compiled. Even if the load or require-library expression loads macro or signature definitions, these will not be loaded as the file is compiled. To fix this problem for macro- and signature-defining files and libraries, wrap each load or require-library expression with (begin-elaboration-time ...), which directs the compiler to evaluate the wrapped expression at compilation time.

For example, suppose that x.ss contains the following Scheme code:

  (require-library "macro.ss") 
  (define f (opt-lambda () 10)) 
If x.ss is loaded directly into MzScheme, f is defined as expected. But if x.ss is compiled to x.so using mzc, and then x.so is loaded into MzScheme, the result is an ``expected procedure, given #<macro>'' exception. The problem is that macro.ss, which defines tha opt-lambda macro, is not loaded until run-time, when x.so is loaded. As a result, the (opt-lambda () 10) expression is compiled as a procedure application. To correct the problem, we wrap the require-library expression with begin-elaboration-time, which instructs mzc to load the library at compile-time:
  (begin-elaboration-time (require-library "macro.ss")) 
  (define f (opt-lambda () 10)) 

mzc's -p or --prefix flag takes a file and loads it at elaboration time before compiling the source files specified on the command line. This is useful for installing a set of macros or signatures that the source files expect to be present already.



PLT