<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Compiling modules that use the Foreign Function Interface</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="The Hugs 98 User's Guide" HREF="index.html"><LINK REL="UP" TITLE="Other ways of running Hugs" HREF="others.html"><LINK REL="PREVIOUS" TITLE="Other ways of running Hugs" HREF="others.html"><LINK REL="NEXT" TITLE="Graphical interface for Windows" HREF="winhugs.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="hugs-ug.css"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >The Hugs 98 User's Guide</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="others.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 4. Other ways of running Hugs</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="winhugs.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="FFIHUGS" >4.2. Compiling modules that use the Foreign Function Interface</A ></H1 ><P ><B CLASS="COMMAND" >ffihugs</B > [<TT CLASS="REPLACEABLE" ><I >option</I ></TT >...] [<CODE CLASS="OPTION" >-i</CODE ><TT CLASS="REPLACEABLE" ><I >include</I ></TT >...] <TT CLASS="REPLACEABLE" ><I >file</I ></TT > [<TT CLASS="REPLACEABLE" ><I >cc-arg</I ></TT >...]</P ><P >Any module that contains <TT CLASS="LITERAL" >foreign import</TT > declarations must be compiled with <B CLASS="COMMAND" >ffihugs</B > before it can be loaded into Hugs.</P ><P >Suppose you have some C functions in <TT CLASS="FILENAME" >test.c</TT > with some <TT CLASS="LITERAL" >foreign import</TT > declarations for those functions in <TT CLASS="FILENAME" >HTest.hs</TT >, and that the code in <TT CLASS="FILENAME" >test.c</TT > needs to be compiled with <CODE CLASS="OPTION" >-lm</CODE >. To use these with Hugs, you must first use <B CLASS="COMMAND" >ffihugs</B > to generate <TT CLASS="FILENAME" >HTest.c</TT >, compile it and link it against <TT CLASS="FILENAME" >test.c</TT > with <CODE CLASS="OPTION" >-lm</CODE > to produce an object file (<TT CLASS="FILENAME" >HTest.so</TT > on Unix, <TT CLASS="FILENAME" >HTest.dll</TT > on Windows): <PRE CLASS="SCREEN" >ffihugs HTest.hs test.c -lm</PRE > Any Hugs options should be placed before the module name, as in <PRE CLASS="SCREEN" >ffihugs -98 HTest.hs test.c -lm</PRE > Now you can run Hugs as normal: <PRE CLASS="SCREEN" >hugs -98 HTest.hs</PRE > When <TT CLASS="FILENAME" >HTest.hs</TT > is loaded, Hugs will load the corresponding object file and then use the imported functions. (If <TT CLASS="FILENAME" >HTest.hs</TT > depends on other modules using foreign functions, you'll have to have compiled those modules too, but not necessarily before compiling <TT CLASS="FILENAME" >HTest.hs</TT >.)</P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B >Because <B CLASS="COMMAND" >ffihugs</B > generates a C file with the same base name as the Haskell source file, any auxiliary C files should avoid that name.</P ></BLOCKQUOTE ></DIV ><P >In the standard FFI, each <TT CLASS="LITERAL" >foreign import</TT > declaration should name a C header file containing the prototype of the function. Because this is often cumbersome, <B CLASS="COMMAND" >ffihugs</B > provides the following additional option:</P ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><A NAME="OPTION-INCLUDE" ></A ><CODE CLASS="OPTION" >-i</CODE ><TT CLASS="REPLACEABLE" ><I >include</I ></TT ></DT ><DD ><P >Specify an include for the generated C file. The <TT CLASS="REPLACEABLE" ><I >include</I ></TT > string should be something that can follow <SPAN CLASS="QUOTE" >"<TT CLASS="LITERAL" >#include</TT >"</SPAN > in a C program, as in <PRE CLASS="SCREEN" >ffihugs '-i<math.h>' '-i"mydefs.h"' Test.hs test.c -lm</PRE > Note the necessary quoting of the <CODE CLASS="OPTION" >-i</CODE > options here.</P ></DD ></DL ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="others.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="winhugs.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Other ways of running Hugs</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="others.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Graphical interface for Windows</TD ></TR ></TABLE ></DIV ></BODY ></HTML >