[Haskell-cafe] Foreign function Intf./GHC newbie question

John Zenger ofzsfd at hotmail.com
Fri Nov 25 21:59:41 EST 2005

Hours of poring over documentation has failed to provide me with the answer 
to what I am sure is a simple question for most of you.  Your help is 

I want to compile a Haskell program that calls a foreign C function.  I'm 
using GHC.  When GHC links, it says it can't find my C function.  That is 
not surprising, because I can't figure out how I'm supposed to tell GHC 
where to find my C function.

This is what I've got:

------ FILE:  ffi.hs
{-# OPTIONS -fglasgow-exts #-}
{-# OPTIONS -#include "myc.h" #-}

module Main where

foreign import ccall "supernumber" supernumber :: Int

bongo :: Int
bongo =
   supernumber + 1

main = do
   putStrLn (show bongo)

------ FILE:  myc.c
static int supernumber()

------ FILE: myc.h
static int supernumber();
------- END OF FILES

When I do "ghc ffi.hs," it comes back:  "ffi.o(.text+0xa8):fake: undefined 
reference to `supernumber' "

I am guessing that I need to tell it where myc.c is.  Perhaps I have to 
compile myc.c first using gcc?  That seems inconvenient.  Any hints?

I am using Windows XP and GHC 6.4, if that matters.

Is your PC infected? Get a FREE online computer virus scan from McAfee® 
Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

More information about the Haskell-Cafe mailing list