views:

283

answers:

3

Hi. I have a problem compiling Haskell programs, that import the Text.Regex.Posix module. I have tried to isolate the problem in a small test program:

module Main () where

import Text.Regex.Posix ((=~))

main = return ()

Running the interpreter works fine:

/regex-test$ runghc Main.hs
/regex-test$

However, compiling this program with ghc yields:

/regex-test$ ghc -o tester Main.hs
Main.o: In function `rmS_info':
(.text+0xf3): undefined reference to `__stginit_regexzmposixzm0zi72zi0zi3_TextziRegexziPosix_'
collect2: ld returned 1 exit status

After this, the interpreter also stops working:

/regex-test$ runghc Main.hs

<interactive>:1:32: Not in scope: `main'
/projects/regex-test$

I can get it to work again by saving the file again without making any changes.

Does anyone have an idea how to solve this?

Some info on my system:

/regex-test$ uname -a
Linux Hello-Ubuntu 2.6.28-15-generic #52-Ubuntu SMP Wed Sep 9 10:49:34 UTC 2009 i686 GNU/Linux

and

/regex-test$ ghc-pkg list
/usr/local/lib/ghc-6.10.3/./package.conf:
Cabal-1.6.0.3, HUnit-1.2.0.3, QuickCheck-1.2.0.0, array-0.2.0.0,
base-3.0.3.1, base-4.1.0.0, bytestring-0.9.1.4, containers-0.2.0.1,
directory-1.0.0.3, (dph-base-0.3), (dph-par-0.3),
(dph-prim-interface-0.3), (dph-prim-par-0.3), (dph-prim-seq-0.3),
(dph-seq-0.3), extensible-exceptions-0.1.1.0, filepath-1.1.0.2,
(ghc-6.10.3), ghc-prim-0.1.0.0, haddock-2.4.2, haskell-src-1.0.1.3,
haskell98-1.0.1.0, hpc-0.5.0.3, html-1.0.1.2, integer-0.1.0.1,
mtl-1.1.0.2, network-2.2.1, old-locale-1.0.0.1, old-time-1.0.0.2,
packedstring-0.1.0.1, parallel-1.1.0.1, parsec-2.1.0.1,
pretty-1.0.1.0, process-1.0.1.1, random-1.0.0.1,
regex-base-0.72.0.2, regex-base-0.93.1, regex-compat-0.71.0.1,
regex-posix-0.72.0.3, regex-tdfa-1.1.2, rts-1.0, stm-2.1.1.2,
syb-0.1.0.1, template-haskell-2.3.0.1, time-1.1.3, unix-2.3.2.0,
xhtml-3000.2.0.1
~/.ghc/i386-linux-6.10.3/package.conf:
HTTP-4000.0.4, zlib-0.5.0.0
+7  A: 

I'd expect at least main to be exported... i.e.

module Main (main) where

but that's not the problem here.

$ ghc -o tester -package regex-posix Main.o

Linking like this works perfectly fine.

You should either use cabal as your buildsystem, compile and link with ghc --make, or take care to expose all requisite packages manually.

ephemient
Thanks, both the --make and -package options solved my problem. I haven't tried cabal yet, but I will soon. I did not export main, since the build example in Real World Haskell, chapter 5, doesn't do it either. Am I right in thinking, that Main modules are never imported by other modules? And if so, given that my program now runs fine without exporting main, what are the differences between exporting and not exporting?
Boris
It still seems odd to me, that the interpreter could find the required package, while the compiler couldn't do it.
Boris
For me, it's a carry-over habit from C, where the runtime will be quite unhappy if `main` has static linkage. Also, you can combine several modules all exporting `main` and use `ghc -main-is` to select between them... not that it's common or recommended, though. In short, it shouldn't really matter, except that I'm more accustomed to it being exported.
ephemient
http://www.haskell.org/ghc/docs/latest/html/users_guide/packages.html#using-packages "In `––make` mode and `––interactive` mode (see Section 4.4, “Modes of operation”), the compiler normally determines which packages are required by the current Haskell modules, and links only those. In batch mode however, the dependency information isn't available, and explicit `-package` options must be given when linking."
ephemient
Thank you for the good clarifying comments.
Boris
+4  A: 

Do exactly what you did but pass --make to ghc as well.

Martijn
Thanks, that solved the problem.
Boris
+3  A: 

You're missing --make flag, which has GHC solve the missing library dependencies for you.

Don Stewart