3.5. Compiling modules that use the Foreign Function Interface

ffihugs [option...] [-iinclude...] file [cc-arg...]

Suppose you have some C functions in test.c with some foreign import declarations for those functions in Test.hs, and that the code in test.c needs to be compiled with -lm. To use these with Hugs, you must first use ffihugs to generate Test.c, compile it and link it against test.c with -lm to produce Test.so:

ffihugs Test.hs test.c -lm
Any Hugs options should be placed before the module name, as in
ffihugs -98 Test.hs test.c -lm
Now you can run Hugs as normal:
hugs -98 Test.hs
When Test.hs is loaded, Hugs will load Test.so and then use the imported functions. (If Test.hs depends on other modules using foreign functions, you'll have to have compiled those modules too, but not necessarily before compiling Test.hs.)

In the standard FFI, each foreign import declaration should name a C header file containing the prototype of the function. Because this is often cumbersome, ffihugs provides the following additional option:

-iinclude

Specify an include for the generated C file. The include string should be something that can follow "#include" in a C program, as in

ffihugs '-i<math.h>' '-i"mydefs.h"' Test.hs test.c -lm
Note the necessary quoting of the +i options here.