4.2. Compiling modules that use the Foreign Function Interface

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

Any module that contains foreign import declarations must be compiled with ffihugs before it can be loaded into Hugs.

Suppose you have some C functions in test.c with some foreign import declarations for those functions in HTest.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 HTest.c, compile it and link it against test.c with -lm to produce an object file (HTest.so on Unix, HTest.dll on Windows):

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

Note: Because ffihugs generates a C file with the same base name as the Haskell source file, any auxiliary C files should avoid that name.

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.