[Haskell-cafe] FFI, C/C++ and undefined references

Malcolm Wallace malcolm.wallace at cs.york.ac.uk
Thu Jan 14 06:14:29 EST 2010


> I still get the "undefined reference" errors.

It is likely there is some combination of other mistakes as well  
then.  Other responses have made suggestions of fixes you require in  
the C++ code for instance.  You will need those as well.

>  I did eventually get ghc to compile
> Main.hs by putting the -c and -cpp flags after "--make Main.hs".
>
> Then it produces a Main.o file which (even with +x permissions on my  
> Linux
> box) will not run. I just get the message "cannot run binary file",  
> or some
> such message.

The file Main.o is just an object file, not a complete executable.  It  
still needs to be linked against some other (Haskell or C/C++) object  
files and libraries, and the Haskell runtime system, to form an  
executable that can be run.  ghc is capable of doing all the linking,  
e.g.
     ghc -o myProg Main.o slirm.o -package base -package foo

However, if you are unsure of which Haskell packages are needed, it is  
wise to let ghc work out the dependencies for you, e.g. with
     ghc --make Main.hs slirm.o

It cannot work out the C/C++ dependencies though, so every time you  
get "undefined reference" linking errors, you must discover which C  
code provides those symbols, and add its object file to the  
commandline by hand.

Regards,
     Malcolm



More information about the Haskell-Cafe mailing list