Wednesday, May 05, 2010

Haskell SDL on Windows, check! (hacking required)

Things looked grim. Even though Timothy could get it to work, the SDL Haskell library wouldn't build for me. libSDL itself installed ok and the samples compiled and ran. But I couldn't compile the hsc files because of a linker error: gcc couldn't find SDLMain or whatever. In desperation, I posted to Haskell-cafe. I didn't get a single answer or hint. I was truly alone.
OK, it was time to either give up or take drastic action. And I never give up.

By then I had narrowed the problem. The order of arguments to gcc were wrong for linking, according to the gcc manual: the .o file should be referenced before the SDL libraries on the command line. It would work like that for other people, but for me, on my system, in my corner of the universe, the order was wrong, and that was it. If I tried the same order of arguments on the SDL samples, they would fail to link. If I reverted the arguments, they would succeed.
So I was left with the straight and narrow: I managed to get the source code for hsc2hs from darcs. I fiddled for a while with the include-dirs and extra-lib options so I could get it to compile and produce the same error than I had with the Haskell Platform version, and not some stupid errors about hsFFI.h not found or so.

Then I opened up, hands trembling and sweat pouring from my forehead, Main.hs in an editor. Found the offending lines:
    rawSystemL ("linking " ++ oProgName) beVerbose linker
( [f | LinkFlag f <- flags]
++ [oProgName]
++ ["-o", progName]

Changed them to:

      rawSystemL ("linking " ++ oProgName) beVerbose linker
( [oProgName]
++ [f | LinkFlag f <- flags]
++ ["-o", progName]
So that the name of the o file would be before the libraries.
Recompiled, copied the exe in the Haskell Platform bin folder...
Relaunched the SDL build...
And watched SDL build and install properly!
Gaped at the first sample of Timothy opening up a blank window that would close when I press a key! (OK, the last print fails, but by now little errors are not going to disturb my feeling of invulnerability).
Danced around my desk!
Had a beer!


Timothy said...


Yea, I definitely did not run into anything like that. What version of Windows were you using? Did you have another copy of mingw or msys installed besides what came with the Haskell Platform?

I did my steps with SDL-0.5.9 and thought maybe 0.5.10 might have broke things, but a diff of the packages shows that the only change is the .cabal file pointing to a different darcs repo.

If it's any consolation, the steps in my post were a result of two days of effort and frustration.

JP Moresmau said...

I'm with SDL-0.5.9 too. I tried both the gcc that comes with the Haskell Platform and the gcc with a different install of MinGW, for the same result. Maybe some env variables missing? At least now I'll be able to look at your tutorials a bit more!

Luke Palmer said...

Congrats! That library's a bear.

Anonymous said...

great msg for me, thanks a lot dude˙﹏˙

Nathan said...

I think I ran into the same problem you did. How did you go about compiling the modified hsc2hs?

JP Moresmau said...

Nathan, the hsc2hs package from darcs is cabalized, so I think I just ran cabal install under MSYS.

Anonymous said...
This comment has been removed by a blog administrator.
Dan said...

I am at a loss. I tried doing what you did, but I get an 'irrefutable pattern match failed' error during build. I'm using Haskell Platform.

I don't know what to do, or where to report this bug so that someone can fix it. It's very frustrating.

Vladimir said...

I'm so glad that I found this post. The same problem made me a bit nervous. After several ours of googling maillists I finally read this. Thank you.

Did you mail the bugfix to developer?

Hartmut said...

In the hackage SDL-0.6.2 package root there is laying a text file called WIN32, which describes (chapter 6) how the missing .so/.DLL for SDLmain error can be worked around alternativly.