[plt-scheme] Extensions

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Nov 13 20:24:02 EST 2006

At Mon, 13 Nov 2006 16:42:24 +0100 (CET), Ivanyi Peter wrote:
> First:
> ------
> Consider the attached files (they are at the back):
> - test.c : This is a standard C file which defines a "test"
> module.
>   In the module there is one function called "test". This
> function
>   sets some flags under Windows to control floating point
> operations.
>   In PLT-Scheme 301 I could use this code however in version
> 352 this
>   code crashes mzscheme. Can someone tell me what has
> changed between
>   the versions? (I need this code for my numerical library
> to signal
>   over and underflow, NaN, etc...)

I think it's because MzScheme itself uses floating-point, and MzScheme
does not handle FP-exception signals. For example, MzScheme may
evaluate `(/ 1 0.0)' internally and expected to get back +inf.0, but if
you unmask FP exceptions, then MzScheme's internal operations generate
FP signals.

If that's it, maybe you could set the control words just before
entering your library, and then restore them on exit? Or maybe you
could install handlers that do the right thing when the exception
occurs within MzScheme? (I have never tried handling FP exceptions, so
I don't know how they work in any detail.)

> Second:
> -------
> I have another problem when I want to build my extension.
> For some reason, I am not sure why, the linker wants to link in
> both libraries: LIBCD.LIB and LIBCMT.LIB .
> I do not know why this is, but I could avoid this by using
> the flag
> /nodefaultlib:libcmt
> However I cannot specify this for mzc, it reports an unknown
> flag.
> So I would like to use something like this:
> mzc ++ldf /nodefaultlib:libcmt ++ldl my.lib -ld test.dll
> test.obj
> but I have to use:
> link /dll /nodefaultlib:libcmt /out:test.dll my.lib
> "c:\Program Files\PLT\lib\msvc\libmzsch352_000.lib" ...
> Any idea how to solve this? Why is that an unknown flag? I
> though mzc accepts
> all flags and just passes them to the compiler or linker.

I'm not sure about this one. Maybe you can show the error message? (I
see that you're missing a "-" in "--ld" above, but I doubt that's the

> Third:
> ------
> I would like to make an executable which "includes" or
> "uses" an extension.
> (However as you can see above the extension is a module.)
> How can I do that?
> The problem is that in DrScheme this is allowed:
> (load-extension "./test.dll")
> (module test mzscheme
>   (require test)
>   (display (test 10))
> )
> but when I try to build it as:
> mzc --exe test.exe test.scm
> mzc reports that it would expect a module, but found
> something else.

You will need to use a different base name for the Scheme code and the
DLL. I'll change your example to

 (module test-wrap mzscheme
   (require "test.scm")
   (display (test 10)))

in a file "test-wrap.scm".

The file "test.scm" won't actually exist. Instead, compile "test.c" to
"test.dll", and put it in the subdirectory

 (build-path "compiled" "native" (system-library-subpath))

This works because the module loader looks for a corresponding ".dll"
in the "compiled/native/..." subdirectory when "test.scm" is requested,
even if "test.scm" doesn't exist (in the same way that it looks for a
".zo" file in "compiled").


Posted on the users mailing list.