Linking ghc .o files with MSVC++

Mauricio Carvalho m.decarvalho@telia.com
Tue, 26 Nov 2002 19:26:04 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0008_01C29581.A9578440
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi
I need help specifically building Haskell functions that I can call from =
C++ (or C).
I looked at the Foreign Function Interface explanation at GHC user's =
guide, but I didn't understand itt well, and I'm not succeeding at doing =
it.=20
I'm using GCH 5.04.1 in Win32 environment, and Microsoft Visual C++
That is as far as I got:
I compiled a simple MyModule.hs file with ghc -c -fffi MyModule.hs
It was just a test module that conatined a factorial function called =
fac.
Here is the source:
module MyModule where
foreign export ccall "fac" fac :: Int -> Int
fac :: Int -> Int
fac n
    | n <=3D 0 =3D 1
    | otherwise =3D n * fac(n-1)
It gave me 4 files: MyModule_stub.h, MyModule_stub.c, MyModule_stub.o, =
MyModule.o (and a MyModule.hi, but I think not relevant for linking with =
C)

I was thinking that a .o and a .h file would be sufficient for the =
linking; anyway, I copied those 4 files to my c++ program directory, =
included the .h file at my c++ main program, add the file =
MyModule_stub.o to the linking process and tried to compile. That is my =
c++ program:
#include <iostream>
#include "MyModule_stub.h"
int main(){
 std::cout << fac(10) << std::endl;
 return 0;
}
The linker complains that some function calls are unresolved:
Compiling...
principal.cpp
Linking...
MyModule_stub.o : error LNK2001: unresolved external symbol =
_MyModule_zdffac_closure
MyModule_stub.o : error LNK2001: unresolved external symbol =
_GHCziTopHandler_runNonIO_closure
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_getInt
MyModule_stub.o : error LNK2001: unresolved external symbol =
_rts_checkSchedStatus
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_mkInt
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_apply
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_evalIO
Debug/templates.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.
I tried to use the file MyModule.o, instead of MyModule_stub.o, and got =
mor unresolved symbols. What was worse, the function symbol itself was =
unresolved:
Linking...
MyModule.o : error LNK2001: unresolved external symbol _getStablePtr
MyModule.o : error LNK2001: unresolved external symbol =
_GHCziNum_Szh_con_info
MyModule.o : error LNK2001: unresolved external symbol =
_stg_upd_frame_info
MyModule.o : error LNK2001: unresolved external symbol =
_GHCziBase_ZMZN_closure
MyModule.o : error LNK2001: unresolved external symbol =
_stg_ap_2_upd_info
MyModule.o : error LNK2001: unresolved external symbol =
_GHCziBase_ZC_con_info
MyModule.o : error LNK2001: unresolved external symbol =
_stg_sel_0_upd_info
MyModule.o : error LNK2001: unresolved external symbol =
_GHCziBase_Izh_static_info
MyModule.o : error LNK2001: unresolved external symbol =
_GHCziReal_zdfIntegralInt_closure
MyModule.o : error LNK2001: unresolved external symbol =
_stg_CAF_BLACKHOLE_info
MyModule.o : error LNK2001: unresolved external symbol _newCAF
MyModule.o : error LNK2001: unresolved external symbol =
_stg_IND_STATIC_info
MyModule.o : error LNK2001: unresolved external symbol =
_stg_INTLIKE_closure
principal.obj : error LNK2001: unresolved external symbol _fac
Debug/templates.exe : fatal error LNK1120: 14 unresolved externals
Error executing link.exe.
Even trying to include both .o modules to the linking process didn't =
help=20
If I link this module to a Haskel program, it works out fine...
So, it seems that when I create an object file from a Haskell module, I =
must link with other object files, that ghc probably links =
automatically. Is it true? An being the case, how can one know which =
object files to link?
Please, understand that I'm quite a newbie using haskell when answering.
Thanks for any help,
Mauricio

------=_NextPart_000_0008_01C29581.A9578440
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2716.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I need help specifically building =
Haskell functions=20
that I can call from C++ (or C).</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I looked at the Foreign Function =
Interface=20
explanation at GHC user's guide, but I didn't understand itt well, and =
I'm not=20
succeeding at doing it. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I'm using GCH 5.04.1 in Win32 =
environment, and=20
Microsoft Visual C++</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>That is as far as I got:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I compiled a simple MyModule.hs file =
with ghc -c=20
-fffi MyModule.hs</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>It was just a test module that =
conatined a=20
factorial function called fac.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Here is the source:</FONT></DIV>
<DIV><FONT size=3D2>module MyModule where</FONT></DIV>
<DIV><FONT size=3D2>foreign export ccall "fac" fac :: Int -&gt; =
Int</FONT></DIV>
<DIV><FONT size=3D2>fac :: Int -&gt; Int</FONT></DIV>
<DIV><FONT size=3D2>fac n</FONT></DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; | n &lt;=3D 0 =3D 1</FONT></DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; | otherwise =3D n * =
fac(n-1)</FONT></DIV>
<DIV><FONT size=3D2></FONT><FONT size=3D2><FONT face=3DArial>It gave me =
4 files:=20
MyModule_stub.h, MyModule_stub.c, MyModule_stub.o, MyModule.o (and a=20
MyModule.hi, but I think not relevant for linking with =
C)</FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2><FONT face=3DArial>I was thinking that a .o and a .h =
file would=20
be sufficient for the linking; anyway, I copied those 4 files to my c++ =
program=20
directory, included the .h file at my c++ main program, add the file=20
MyModule_stub.o to the linking process and tried to compile. That is my =
c++=20
program:</FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3DArial>#include =
&lt;iostream&gt;<BR>#include=20
"MyModule_stub.h"</FONT></FONT><FONT size=3D2><FONT face=3DArial><BR>int =

main(){<BR>&nbsp;std::cout &lt;&lt; fac(10) &lt;&lt; =
std::endl;<BR>&nbsp;return=20
0;</DIV>
<DIV>}</DIV>
<DIV>The linker complains that some function calls are unresolved:</DIV>
<DIV><FONT =
size=3D1>Compiling...<BR>principal.cpp<BR>Linking...<BR>MyModule_stub.o=20
: error LNK2001: unresolved external symbol=20
_MyModule_zdffac_closure<BR>MyModule_stub.o : error LNK2001: unresolved =
external=20
symbol _GHCziTopHandler_runNonIO_closure<BR>MyModule_stub.o : error =
LNK2001:=20
unresolved external symbol _rts_getInt<BR>MyModule_stub.o : error =
LNK2001:=20
unresolved external symbol _rts_checkSchedStatus<BR>MyModule_stub.o : =
error=20
LNK2001: unresolved external symbol _rts_mkInt<BR>MyModule_stub.o : =
error=20
LNK2001: unresolved external symbol _rts_apply<BR>MyModule_stub.o : =
error=20
LNK2001: unresolved external symbol _rts_evalIO<BR>Debug/templates.exe : =
fatal=20
error LNK1120: 7 unresolved externals<BR>Error executing =
link.exe.</FONT></DIV>
<DIV>I tried to use the file MyModule.o, instead of MyModule_stub.o, and =
got mor=20
unresolved symbols. What was worse, the function symbol itself was=20
unresolved:</DIV>
<DIV><FONT size=3D1>Linking...<BR>MyModule.o : error LNK2001: unresolved =
external=20
symbol _getStablePtr<BR>MyModule.o : error LNK2001: unresolved external =
symbol=20
_GHCziNum_Szh_con_info<BR>MyModule.o : error LNK2001: unresolved =
external symbol=20
_stg_upd_frame_info<BR>MyModule.o : error LNK2001: unresolved external =
symbol=20
_GHCziBase_ZMZN_closure<BR>MyModule.o : error LNK2001: unresolved =
external=20
symbol _stg_ap_2_upd_info<BR>MyModule.o : error LNK2001: unresolved =
external=20
symbol _GHCziBase_ZC_con_info<BR>MyModule.o : error LNK2001: unresolved =
external=20
symbol _stg_sel_0_upd_info<BR>MyModule.o : error LNK2001: unresolved =
external=20
symbol _GHCziBase_Izh_static_info<BR>MyModule.o : error LNK2001: =
unresolved=20
external symbol _GHCziReal_zdfIntegralInt_closure<BR>MyModule.o : error =
LNK2001:=20
unresolved external symbol _stg_CAF_BLACKHOLE_info<BR>MyModule.o : error =

LNK2001: unresolved external symbol _newCAF<BR>MyModule.o : error =
LNK2001:=20
unresolved external symbol _stg_IND_STATIC_info<BR>MyModule.o : error =
LNK2001:=20
unresolved external symbol _stg_INTLIKE_closure<BR>principal.obj : error =

LNK2001: unresolved external symbol _fac<BR>Debug/templates.exe : fatal =
error=20
LNK1120: 14 unresolved externals<BR>Error executing =
link.exe.</FONT></DIV>
<DIV>Even trying to include both .o modules to the linking process =
didn't help=20
</DIV>
<DIV>If I link this module to a Haskel program, it works out =
fine...</DIV>
<DIV>So, it seems that when I create an object file from a Haskell =
module, I=20
must link with other object files, that ghc probably links =
automatically. Is it=20
true? An being the case, how can one know which object files to =
link?</DIV>
<DIV>Please, understand that I'm quite a newbie using haskell when=20
answering.</DIV>
<DIV>Thanks for any help,</DIV>
<DIV>Mauricio</FONT></DIV></FONT></BODY></HTML>

------=_NextPart_000_0008_01C29581.A9578440--