views:

260

answers:

2

How to tell ghc to tell ld to link compiled binaries to SDL library?

I have a source.hs :

    import Prelude
    import Graphics.UI.SDL as SDL
    import Data.Maybe
    import GHC.Word
    import Control.Applicative
    ...

When I do:

    ghc source.hs

I get a bunch of linking errors similar to this one:

    pong.o: In function `s1Ww_info':
    (.text+0x449): undefined reference to `SDLzm0zi5zi9_GraphicsziUIziSDLziRect_Rect_con_info'

What am I doing wrong?

+3  A: 

Add --make, which includes the linking phase.

Don Stewart
+4  A: 

If for some reason you don't want to use GHC's --make option, this should work: ghc source.hs -lSDL -package SDL

If you want some of the non-core SDL sub-libraries, you'll have to include those separately, e.g., ghc source.hs -lSDL -SDL_ttf -package SDL -package SDL-ttf

You may also want to consider setting up a build file using cabal, the Haskell packaging system, especially if your program expands beyond a couple source files.

And a word of warning--you didn't mention what operating system you're using, but last time I tried Haskell's SDL bindings only worked "out of the box" on Linux--both Windows and OS X cause it problems, due to an ugly hack that SDL uses when starting itself on those platforms.

camccann
@camccann: I'm on Linux. The make thing worked. It's just I'm slightly confused why the sources I used to compile before worked without --make. I guess it's because there were no import statements, right?
Alex
No import statements that pulled in non-core packages, at least; the package "base" is automatically linked by default, and contains all the other modules you're importing there, plus many others.
camccann
Oh, and one last bit of unsolicited advice: `Graphics.UI.SDL` has minimal documentation, but for the most part it's just a thin wrapper around SDL's C API to use Haskell types. So, if you need reference material for using it, anything in C should translate over pretty easily. Have fun!
camccann