Error Interpreting Main with API

Hello, I am trying to use the API to interpret a Main module and get some type information from it. However, everytime I try to load a "Main.hs" module using the API I get the following error message: /usr/local/packages/ghc-6.8.2/lib/ghc-6.8.2/libHSrts.a(Main.o): In function `real_main': Main.c:(.text+0x7): undefined reference to `__stginit_ZCMain' Main.c:(.text+0x36): undefined reference to `ZCMain_main_closure' collect2: ld returned 1 exit status If I create an empty file called "Main" within the directory where "Main.hs" is located the program suceeds, I'm guessing this is because there is no linking involved. Is there an extra flag I need to pass to GHC in order to avoid this error? It works perfectly if I try it on a module that is not called Main. main = defaultErrorHandler defaultDynFlags $ do let packageConf = "/usr/local/packages/ghc-6.8.2/lib/ghc-6.8.2" session <- GHC.newSession {-JustTypecheck-} (Just (filter (/= '\n') packageConf)) dflags0 <- getSessionDynFlags session (f1,b) <- parseDynamicFlags dflags0 ["-fglasgow-exts"] setSessionDynFlags session f1{verbosity=6, hscTarget = HscInterpreted} target <- GHC.guessTarget "/home/cmb21//HaRe_Project/testing/subIntroPattern/Main.hs" Nothing hscTarget=HscNothing} GHC.addTarget session target GHC.load session GHC.LoadAllTargets usermod <- findModule session (mkModuleName "Main") Nothing setContext session [usermod] [] return session I hope somebody can share some light! Kind regards, Chris.

C.M.Brown wrote:
Hello,
I am trying to use the API to interpret a Main module and get some type information from it. However, everytime I try to load a "Main.hs" module using the API I get the following error message:
/usr/local/packages/ghc-6.8.2/lib/ghc-6.8.2/libHSrts.a(Main.o): In function `real_main': Main.c:(.text+0x7): undefined reference to `__stginit_ZCMain' Main.c:(.text+0x36): undefined reference to `ZCMain_main_closure' collect2: ld returned 1 exit status
If I create an empty file called "Main" within the directory where "Main.hs" is located the program suceeds, I'm guessing this is because there is no linking involved. Is there an extra flag I need to pass to GHC in order to avoid this error? It works perfectly if I try it on a module that is not called Main.
The GHC API is behaving just like --make: it links the program if you have a Main module. To ask it not to link, you want to do the same as '--make -c', which in the GHC API means setting the ghcLink field of DynFlags to NoLink. Cheers, Simon

Simon,
The GHC API is behaving just like --make: it links the program if you have a Main module. To ask it not to link, you want to do the same as '--make -c', which in the GHC API means setting the ghcLink field of DynFlags to NoLink.
Thanks, this has solved the problem I was having. I wonder whether it would be a good idea to amend the Wiki page to relect this? I think it is reasonable to assume that other users are going to want to use the API against Main modules and it's possibly not too clear at the moment how to do this. Regards, Chris.

| > The GHC API is behaving just like --make: it links the program if you | > have a Main module. To ask it not to link, you want to do the same as | > '--make -c', which in the GHC API means setting the ghcLink field of | > DynFlags to NoLink. | | Thanks, this has solved the problem I was having. I wonder whether it | would be a good idea to amend the Wiki page to relect this? I think it is | reasonable to assume that other users are going to want to use the API | against Main modules and it's possibly not too clear at the moment how to | do this. Yes I do, please! Would you feel up to doing so? I'm sure Simon'd check your text for veracity! thanks Simon

Hi Simon,
Yes I do, please! Would you feel up to doing so? I'm sure Simon'd check your text for veracity!
I have modified the wiki, and added a new section under "initialisation": http://www.haskell.org/haskellwiki/GHC/As_a_library Would it also be appropriate to modify that wiki page to reflect some of the changes between 6.6.1 and 6.8.2? I don't mind doing this at some point today or tomorrow... Regards, Chris.

| Would it also be appropriate to modify that wiki page to reflect some of | the changes between 6.6.1 and 6.8.2? I don't mind doing this at some point | today or tomorrow... Yes please, that would be vv helpful. S

Simon Peyton-Jones wrote:
| Would it also be appropriate to modify that wiki page to reflect some of | the changes between 6.6.1 and 6.8.2? I don't mind doing this at some point | today or tomorrow...
Yes please, that would be vv helpful.
Right; the page currently has a mixture of 6.6 and 6.8-style examples of the API. In particular, the GhcMode thing is completely gone in 6.8, and hence newSession only takes one argument. I suspect many of the examples don't work because they're using a mixture of the two APIs. Anyway, Thomas Schilling's summer of code project should address all this: he'll be writing some proper documentation for the API. Cheers, Simon
participants (3)
-
C.M.Brown
-
Simon Marlow
-
Simon Peyton-Jones