
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?

Hi
On 9/27/06, Matthew Bromberg
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

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
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
-- View this message in context: http://www.nabble.com/Creating-DLLs-with-GHC-tf2342692.html#a6529535 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On 9/26/06, Matthew Bromberg
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

Jason Dagit wrote:
On 9/26/06, Matthew Bromberg
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. -- View this message in context: http://www.nabble.com/Creating-DLLs-with-GHC-tf2342692.html#a6534625 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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:Bulat.Ziganshin@gmail.com

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. -- View this message in context: http://www.nabble.com/Creating-DLLs-with-GHC-tf2342692.html#a6536255 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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

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
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
#include 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); -- View this message in context: http://www.nabble.com/Creating-DLLs-with-GHC-tf2342692.html#a6539496 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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? -- View this message in context: http://www.nabble.com/Creating-DLLs-with-GHC-tf2342692.html#a6559542 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
participants (5)
-
Bulat Ziganshin
-
Esa Ilari Vuokko
-
Jason Dagit
-
Matthew Bromberg
-
SevenThunders