Creating DLLs with GHC

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating DLLs with GHC

SevenThunders
I am having some difficulty with creating a dynamic link library using
GHC on windows XP.

I am attempting to follow the example in
http://www.haskell.org/ghc/docs/6.4/html/users_guide/win32-dlls.html

though I have a binary build of ghc 6.5

My problem (I think) is that some of my Haskell source files link to an
external C library also contained in a DLL.

I generate my Haskell object files when I compile my Haskell executable.
I then attempt to use the object files in a ghc compiler statement like
this,

ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."

My external C library is in matrixstack.dll and it has a corresponding
static link stub library matrixstack.lib in the same directory as all
the sources. It has references in one of the sources (say src1.hs).

Unfortunately I get a host of undefined references to basically all the
functions in matrixstack.dll and also some undefined references of the form
Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
`TextziParserCombinatorsziParsecziError_show_closure'
Parsefile.o:ghc2996_0.hc:(.text+0x220): undefined reference to
`TextziParserCombinatorsziParsecziChar_spaces_closure'
Parsefile.o:ghc2996_0.hc:(.text+0x24a): undefined reference to
`TextziParserCombinatorsziParsecziChar_spaces_closure'
Parsefile.o:ghc2996_0.hc:(.text+0x29c): undefined reference to
`TextziParserCombinatorsziParsecziChar_spaces_closure'
...

This appears to be coming from unsatisfied references to
Text.ParserCombinators.Parsec, which I thought was a standard library.  
I note that my stand alone haskell executable links just fine and runs
perfectly.

What am I missing to make this work?


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

Esa Ilari Vuokko
Hi

On 9/27/06, Matthew Bromberg <[hidden email]> wrote:
> though I have a binary build of ghc 6.5

If you have new ghc 6.5, you can use --mk-dll with --make, in case that helps.

> ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
> src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."
>
> My external C library is in matrixstack.dll and it has a corresponding
> static link stub library matrixstack.lib in the same directory as all
> the sources. It has references in one of the sources (say src1.hs).

If I recall correctly, to link Ms-style import libs, you must specify
them as object
files (ie no -optl-lfoo, but just foo.lib) or make mingw-style import
libs via .def
files.  But as you've not run into this yet, maybe it works for you like that.

> Unfortunately I get a host of undefined references to basically all the
> functions in matrixstack.dll and also some undefined references of the form
> Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
> `TextziParserCombinatorsziParsecziError_show_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x220): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x24a): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x29c): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'

Add
-package parsec
to you ghc commandline.  And similary for any other packages you used while
compiling.

HTH,
Esa
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

Cyril Schmidt
In reply to this post by SevenThunders
Matthew,

As regards the symbols that end with _closure, I believe you can
resolve them by adding
-package parsec
to the ghc command line (as far as I can see, all the symbols you
list come from the parsec package).

I don't know, though, what to do with the undefined symbols from
matrixstack. You can try to check two things:
1) That the options you specify with -optl really reach the linker
   (e.g. try to give a non-existent library in -optl option and see
   if it complains).
2) That matrixstack.lib is understood by the linker (that is, it
   must be created with dlltool)
3) That the symbols it is looking for do exist in matrixstack.lib
   (you can check it with objdump, for instance).

Cheers,

Cyril
___

Matthew Bromberg wrote:

>ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
>src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."

>My external C library is in matrixstack.dll and it has a corresponding
>static link stub library matrixstack.lib in the same directory as all
>the sources. It has references in one of the sources (say src1.hs).

>Unfortunately I get a host of undefined references to basically all the
>functions in matrixstack.dll and also some undefined references of the form
>Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
>`TextziParserCombinatorsziParsecziError_show_closure'

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders
In reply to this post by Esa Ilari Vuokko

Well I tried this statement
ghc --mk-dll -fglasgow-exts -fffi -I. --make ExternLib.hs

It only compiled the object file, creating ExternLib.o, but it did not create the stub file or attempt to link in the dependent packages.  I then went back to this,

ghc --mk-dll -fglasgow-exts -fffi -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o src1_stub.o src2.o -package parsec -optl-lmatrixstack -optl-L"."

This removed the undefined references to the parsec package, however I still can't get it to link to the matrixstack.lib and matrixstack.dll.  matrixstack.lib was created using dlltool and does link just fine if linking the sources into a stand alone executable instead of a .dll.  I did try to include matrixstack.lib on the command line directly as well but that ended up with the same problem.

Is it possible to just compile everything into C?  I'd need to link to the haskell runtime libraries somehow.



Esa Ilari Vuokko wrote
Hi

On 9/27/06, Matthew Bromberg <mattcbro@earthlink.net> wrote:
> though I have a binary build of ghc 6.5

If you have new ghc 6.5, you can use --mk-dll with --make, in case that helps.

> ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
> src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."
>
> My external C library is in matrixstack.dll and it has a corresponding
> static link stub library matrixstack.lib in the same directory as all
> the sources. It has references in one of the sources (say src1.hs).

If I recall correctly, to link Ms-style import libs, you must specify
them as object
files (ie no -optl-lfoo, but just foo.lib) or make mingw-style import
libs via .def
files.  But as you've not run into this yet, maybe it works for you like that.

> Unfortunately I get a host of undefined references to basically all the
> functions in matrixstack.dll and also some undefined references of the form
> Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
> `TextziParserCombinatorsziParsecziError_show_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x220): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x24a): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x29c): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'

Add
-package parsec
to you ghc commandline.  And similary for any other packages you used while
compiling.

HTH,
Esa
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

Jason Dagit
In reply to this post by SevenThunders
On 9/26/06, Matthew Bromberg <[hidden email]> wrote:

> I am having some difficulty with creating a dynamic link library using
> GHC on windows XP.
>
> I am attempting to follow the example in
> http://www.haskell.org/ghc/docs/6.4/html/users_guide/win32-dlls.html
>
> though I have a binary build of ghc 6.5
>
> My problem (I think) is that some of my Haskell source files link to an
> external C library also contained in a DLL.
>
> I generate my Haskell object files when I compile my Haskell executable.
> I then attempt to use the object files in a ghc compiler statement like
> this,
>
> ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
> src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."
>
> My external C library is in matrixstack.dll and it has a corresponding
> static link stub library matrixstack.lib in the same directory as all
> the sources. It has references in one of the sources (say src1.hs).
>
> Unfortunately I get a host of undefined references to basically all the
> functions in matrixstack.dll and also some undefined references of the form
> Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
> `TextziParserCombinatorsziParsecziError_show_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x220): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x24a): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x29c): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> ...

If you use cabal I think can follow an example I put on the wiki to
get these undefined references to go away:
http://www.haskell.org/haskellwiki/Cabal

Jason
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders

Jason Dagit wrote
On 9/26/06, Matthew Bromberg <mattcbro@earthlink.net> wrote:
> I am having some difficulty with creating a dynamic link library using
> GHC on windows XP.
>
> I am attempting to follow the example in
> http://www.haskell.org/ghc/docs/6.4/html/users_guide/win32-dlls.html
>
> though I have a binary build of ghc 6.5
>
> My problem (I think) is that some of my Haskell source files link to an
> external C library also contained in a DLL.
>
> I generate my Haskell object files when I compile my Haskell executable.
> I then attempt to use the object files in a ghc compiler statement like
> this,
>
> ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
> src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."
>
> My external C library is in matrixstack.dll and it has a corresponding
> static link stub library matrixstack.lib in the same directory as all
> the sources. It has references in one of the sources (say src1.hs).
>
> Unfortunately I get a host of undefined references to basically all the
> functions in matrixstack.dll and also some undefined references of the form
> Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
> `TextziParserCombinatorsziParsecziError_show_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x220): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x24a): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> Parsefile.o:ghc2996_0.hc:(.text+0x29c): undefined reference to
> `TextziParserCombinatorsziParsecziChar_spaces_closure'
> ...

If you use cabal I think can follow an example I put on the wiki to
get these undefined references to go away:
http://www.haskell.org/haskellwiki/Cabal

Jason
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Does cabal really work on windows?  Although it's installed I notice that when I try to build my library using it, it dies on the first foreign import statement in the first .hs source it tries to compile. It is reminiscent of trying to import a foreign C module directly ghci.  ghc.exe, on the other hand has no problem with it.  

Does this scheme address the problem of being unable to link external C libraries (a DLL) into the Haskell DLL?  Are there any other workarounds here?  Is it possible to create static link libraries compatible with MS VC++?  Ultimately I'm trying to link my C code with Matlab.  The C code stub will then call a bunch of Haskell routines.
Reply | Threaded
Open this post in threaded view
|

Creating DLLs with GHC

Jason Dagit
On 9/27/06, SevenThunders <[hidden email]> wrote:

> Does cabal really work on windows?

I've never had a problem with cabal on windows.  I use it instead of
makefiles and I'm reasonably happy with it.

> Although it's installed I notice that
> when I try to build my library using it, it dies on the first foreign import
> statement in the first .hs source it tries to compile. It is reminiscent of
> trying to import a foreign C module directly ghci.  ghc.exe, on the other
> hand has no problem with it.

Perhaps your .cabal file is not correct or your cabal version is too
old?  It's very hard to say without detailed error messages and a
clear description of what you're doing (with sample code).

> Does this scheme address the problem of being unable to link external C
> libraries (a DLL) into the Haskell DLL?  Are there any other workarounds
> here?

I only create a DLL from haskell and link that in a C++ application.
I haven't tried the other way.  Probably over the next month or so the
DLL I'm creating in Haskell will need to access an existing DLL.
Hopefully this will work fine.

> Is it possible to create static link libraries compatible with MS
> VC++?  Ultimately I'm trying to link my C code with Matlab.  The C code stub
> will then call a bunch of Haskell routines.

I have no idea.  I'm fairly new to using Haskell in windows.

Jason
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders

Jason Dagit wrote
On 9/27/06, SevenThunders <mattcbro@earthlink.net> wrote:

> Does cabal really work on windows?

I've never had a problem with cabal on windows.  I use it instead of
makefiles and I'm reasonably happy with it.

> Although it's installed I notice that
> when I try to build my library using it, it dies on the first foreign import
> statement in the first .hs source it tries to compile. It is reminiscent of
> trying to import a foreign C module directly ghci.  ghc.exe, on the other
> hand has no problem with it.

Perhaps your .cabal file is not correct or your cabal version is too
old?  It's very hard to say without detailed error messages and a
clear description of what you're doing (with sample code).

> Does this scheme address the problem of being unable to link external C
> libraries (a DLL) into the Haskell DLL?  Are there any other workarounds
> here?

I only create a DLL from haskell and link that in a C++ application.
I haven't tried the other way.  Probably over the next month or so the
DLL I'm creating in Haskell will need to access an existing DLL.
Hopefully this will work fine.

> Is it possible to create static link libraries compatible with MS
> VC++?  Ultimately I'm trying to link my C code with Matlab.  The C code stub
> will then call a bunch of Haskell routines.

I have no idea.  I'm fairly new to using Haskell in windows.

Jason
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Actually I've had no problems linking external C DLLs into Haskell.  I suspect that I can even create Haskell DLLs provided I'm not also externally linking a C library.  The problem appears to be doing both at the same time.  That is creating a DLL from Haskell source that also links to an external DLL.  Right now it seems that something strange is happening with the linker.

I have this statement approximately
ghc --mk-dll -fglasgow-exts -fffi -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o src1_stub.o src2.o -package parsec -optl-lmatrixstack -optl-L"."

matrixstack.lib was created with dlltool.exe on windows so it should be gcc compliant.  Now I've tried the following variations with the -optl-l argument.

-optl-lcheese

compiler doesn't complain about the cheese library not existing (matrixlib links still unresolved of course).  This is a bad sign.

-lmatrixstack

compiler complains that matrixstack doesn't exist.  This is true even if I create
libmatrixstack.a
matrixstack.a
libmatrixstack.lib
matrixstack.lib

If I add a -L"." argument (instead of optl-L) I get the same behavior as the original case.  No complaints about not finding matrixstack.lib, but a bunch of unresolved references from this library.  In the latter case, however a -lcheese argument does result in an error saying it can't find cheese.

 Keep in mind that compiling the standalone executable (without the --mk-dll flag) works just fine with the original  -optl  options linking to the matrixstack.lib library.
Could this be a bug with my version of ghc 6.5, or perhaps a known issue?

There is some statement about not being able to create multiple DLLs for a single project.  Am I running up against such an issue?  This would be surprising since presumably the compiled C code in the static library matrixstack.lib performs the external DLL access independent from what Haskell is doing.
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders
In reply to this post by SevenThunders

SevenThunders wrote
I am having some difficulty with creating a dynamic link library using
GHC on windows XP.

I am attempting to follow the example in
http://www.haskell.org/ghc/docs/6.4/html/users_guide/win32-dlls.html

though I have a binary build of ghc 6.5

My problem (I think) is that some of my Haskell source files link to an
external C library also contained in a DLL.

I generate my Haskell object files when I compile my Haskell executable.
I then attempt to use the object files in a ghc compiler statement like
this,

ghc --mk-dll -o netsim.dll ExternLib.o ExternLib_stub.o dllNet.o src1.o
src1_stub.o src2.o  -optl-lmatrixstack -optl-L"."

My external C library is in matrixstack.dll and it has a corresponding
static link stub library matrixstack.lib in the same directory as all
the sources. It has references in one of the sources (say src1.hs).

Unfortunately I get a host of undefined references to basically all the
functions in matrixstack.dll and also some undefined references of the form
Parsefile.o:ghc2996_0.hc:(.text+0x130): undefined reference to
`TextziParserCombinatorsziParsecziError_show_closure'
Parsefile.o:ghc2996_0.hc:(.text+0x220): undefined reference to
`TextziParserCombinatorsziParsecziChar_spaces_closure'
Parsefile.o:ghc2996_0.hc:(.text+0x24a): undefined reference to
`TextziParserCombinatorsziParsecziChar_spaces_closure'
Parsefile.o:ghc2996_0.hc:(.text+0x29c): undefined reference to
`TextziParserCombinatorsziParsecziChar_spaces_closure'
...

This appears to be coming from unsatisfied references to
Text.ParserCombinators.Parsec, which I thought was a standard library.  
I note that my stand alone haskell executable links just fine and runs
perfectly.

What am I missing to make this work?


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

I have solved one issue here.  Apparently my matrixstack.lib was corrupted.  I am not sure how this happened. Recompiling this library seems to have resolved the problem concerning undefined links.  Sorry for the diversion and I do appreciate the help offered.
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders
In reply to this post by SevenThunders

SevenThunders wrote
I am having some difficulty with creating a dynamic link library using
GHC on windows XP.

I need to report some additional strange DLL behavior with ghc.exe unfortunately.

Although I solved my linking problems and was able to create a .dll and a MS VC .lib file using  a .def file.
I get a nasty run time error when my program exits.  

Here is a snippet of the  Haskell code:
module ExternLib where

...
import Foreign.C.String
import Foreign.Ptr
import Foreign.C.Types (CInt, CDouble )
import Foreign.Marshal.Array
import Foreign.Storable

foreign export stdcall initNetChan :: CString -> Ptr CInt -> IO ()


-- | initialize network parameters and return an integer array containing
-- indices to the uplink channel, downlink channel
initNetChan :: CString -> Ptr CInt -> IO()
initNetChan simstring cptr = do

-- some processing
...

        let hup = mkCInt $ hupchan netop
        let hdn = mkCInt $ hdnchan netop
        print $ "hup = " ++ (show hup)
        print $ "hdn = " ++ (show hdn)
        -- write results to the output array
        pokeElemOff cptr 0 hup
        pokeElemOff cptr 1 hdn
        peekElemOff cptr 0 >>= print

Here is the C code that calls it, (test.c)

#include <stdio.h>

extern void initNetChan(char *str, int *iout) ;

int zout[64] ;


int main(int argc, char *argv[])
{
printf("Starting initNetChan\n") ;
initNetChan("SimPrams.in", zout) ;
printf("Done initNetChan.  out: %p\n", zout) ;
printf("out[0] = %d out[1] = %d\n", zout[0], zout[1]) ;
printf("Done") ;
return(1) ;
}


The dll itself uses this template taken from the GHC manual on DLLs
#include <windows.h>
#include <Rts.h>

extern void __stginit_ExternLib(void);

static char* args[] = { "ghcDll", NULL };
                       /* N.B. argv arrays must end with NULL */
BOOL
STDCALL
DllMain
   ( HANDLE hModule
   , DWORD reason
   , void* reserved
   )
{
  if (reason == DLL_PROCESS_ATTACH) {
      /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
      startupHaskell(1, args, __stginit_ExternLib);
      return TRUE;
  }
  return TRUE;
}


I link test.c to my dll via a call
cl.exe test.c netsim.lib

Running test.exe yields
Starting initNetChan
"hup = 26"
"hdn = 30"
26
Done initNetChan.  out: 00408960
out[0] = 26 out[1] = 30
Done
D:\Projects\BRPhoenix\NetworkSim\FastSim>test.exe
Starting initNetChan
"hup = 26"
"hdn = 30"
26
Done initNetChan.  out: 00408960
out[0] = 26 out[1] = 30
Done

which is correct, but then the code crashes with a run time error.  It is an unhandled exception: access violation.
Is it possible that the Haskell code needs to do some kind of finalization process before terminating?
I haven't seen the documentation for it yet.  Hopefully it's just something stupid I've done, but again I am baffled.

Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders

SevenThunders wrote
SevenThunders wrote
I am having some difficulty with creating a dynamic link library using
GHC on windows XP.

I need to report some additional strange DLL behavior with ghc.exe unfortunately.

Although I solved my linking problems and was able to create a .dll and a MS VC .lib file using  a .def file.
I get a nasty run time error when my program exits.  

Here is a snippet of the  Haskell code:
module ExternLib where

...
import Foreign.C.String
import Foreign.Ptr
import Foreign.C.Types (CInt, CDouble )
import Foreign.Marshal.Array
import Foreign.Storable

foreign export stdcall initNetChan :: CString -> Ptr CInt -> IO ()


-- | initialize network parameters and return an integer array containing
-- indices to the uplink channel, downlink channel
initNetChan :: CString -> Ptr CInt -> IO()
initNetChan simstring cptr = do

-- some processing
...

        let hup = mkCInt $ hupchan netop
        let hdn = mkCInt $ hdnchan netop
        print $ "hup = " ++ (show hup)
        print $ "hdn = " ++ (show hdn)
        -- write results to the output array
        pokeElemOff cptr 0 hup
        pokeElemOff cptr 1 hdn
        peekElemOff cptr 0 >>= print

Here is the C code that calls it, (test.c)

#include <stdio.h>

extern void initNetChan(char *str, int *iout) ;

int zout[64] ;


int main(int argc, char *argv[])
{
printf("Starting initNetChan\n") ;
initNetChan("SimPrams.in", zout) ;
printf("Done initNetChan.  out: %p\n", zout) ;
printf("out[0] = %d out[1] = %d\n", zout[0], zout[1]) ;
printf("Done") ;
return(1) ;
}


The dll itself uses this template taken from the GHC manual on DLLs
#include <windows.h>
#include <Rts.h>

extern void __stginit_ExternLib(void);

static char* args[] = { "ghcDll", NULL };
                       /* N.B. argv arrays must end with NULL */
BOOL
STDCALL
DllMain
   ( HANDLE hModule
   , DWORD reason
   , void* reserved
   )
{
  if (reason == DLL_PROCESS_ATTACH) {
      /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
      startupHaskell(1, args, __stginit_ExternLib);
      return TRUE;
  }
  return TRUE;
}


I link test.c to my dll via a call
cl.exe test.c netsim.lib

Running test.exe yields
Starting initNetChan
"hup = 26"
"hdn = 30"
26
Done initNetChan.  out: 00408960
out[0] = 26 out[1] = 30
Done
D:\Projects\BRPhoenix\NetworkSim\FastSim>test.exe
Starting initNetChan
"hup = 26"
"hdn = 30"
26
Done initNetChan.  out: 00408960
out[0] = 26 out[1] = 30
Done

which is correct, but then the code crashes with a run time error.  It is an unhandled exception: access violation.
Is it possible that the Haskell code needs to do some kind of finalization process before terminating?
I haven't seen the documentation for it yet.  Hopefully it's just something stupid I've done, but again I am baffled.
Well I'm batting 1000 today.  It was my dumb fault again.  In case this helps someone else,  I forgot to use the --stdcall prefix to the function declaration.  Thus my stack was trashed.

I needed to use a declaration something like
__declspec(dllimport) void  __stdcall initNetChan(HsPtr a1, HsPtr a2);

Reply | Threaded
Open this post in threaded view
|

Re[2]: Creating DLLs with GHC

Bulat Ziganshin-2
In reply to this post by SevenThunders
Hello SevenThunders,

Thursday, September 28, 2006, 12:28:45 AM, you wrote:
> Does cabal really work on windows?

i use it since ghc 6.4.2

> Although it's installed I notice that
> when I try to build my library using it, it dies on the first foreign import
> statement in the first .hs source it tries to compile.

i think that it may need -fvia-C. although i'm not sure, try to
prepend this line to module:

{-# OPTIONS_GHC -cpp -fvia-C -fglasgow-exts #-}

> It is reminiscent of
> trying to import a foreign C module directly ghci.  ghc.exe, on the other
> hand has no problem with it.  

look at http://www.haskell.org/library/ArrayRef.tar.gz

the files you need to steal (and edit) are
LICENSE
ArrayRef.cabal
Setup.hs

although for test try to just "make" it



--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Creating DLLs with GHC

SevenThunders
In reply to this post by SevenThunders
SevenThunders wrote
I am having some difficulty with creating a dynamic link library using
GHC on windows XP.

I am having some problems with GHCs stdout when a Haskell program is called from a windows program.


As I noted earlier I am calling some Haskell code from C as a bridge to being able to
ultimately call Haskell from Matlab 6.5.

The Haskell code is compiled into a .DLL file on a windows machine.
Matlab calls some C code which then calls the Haskell code.

As soon as it gets into the Haskell code I get this run time error in ghcDLL.dll

<stdout>: hPutChars: invalid argument (Bad File Descriptor)

The Haskell code seems to work correctly if called from a stand-alone C program.

Matlab does not properly redirect stdout so any printf calls from the C code simply fail to print.
However Haskell's behavior is to halt after generating a runtime error.

The C code used to generate the Haskell .dll is of course mingw gcc, but the C code used to create my Matlab mex file is Microsoft VC++ 6.0.  I tried to redirect stdout using freopen() in C, but that never seems to work with Microsoft.  At any rate it certainly doesn't effect Haskells use of stdout.  I think in general for windows programs there is no stdout defined, whereas it's always defined for console programs.

My question is, is there some way I can redirect stdout from inside Haskell so that all output is sent to a file?  Is there an equivalent of freopen() in Haskell that works?