<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Verdana;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:blue;
        text-decoration:underline;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:Verdana;
        color:blue;
        font-weight:normal;
        font-style:normal;
        text-decoration:none none;}
@page Section1
        {size:595.3pt 841.9pt;
        margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.Section1
        {page:Section1;}
-->
</style>

</head>

<body lang=EN-GB link=blue vlink=blue>

<div class=Section1>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'>There&#8217;s been a bit of traffic
about calling Haskell from C++ recently, and I know that calling C++ from
Haskell is a frequently asked question. (E.g. Koen Claessen was asking me about
it this week.)&nbsp; It also seems to be somewhat platform dependent.&nbsp;
(Esp on Windows, I think.)<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Would
someone like to write up a description of how <o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to
successfully call out from Haskell to C++ and vice versa, <o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using
GHC?&nbsp; <o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'>If you did, we&#8217;d gladly include the
result in the user manual, and save others a lot of head scratching.&nbsp; Maybe
there are simple things we could do to GHC to make the process simpler.&nbsp;
But what it needs is someone who knows C++ well to write a coherent story.&nbsp;
Please!<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'>Simon<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 color=blue face=Verdana><span style='font-size:
10.0pt;font-family:Verdana;color:blue'><o:p>&nbsp;</o:p></span></font></p>

<div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'>

<div>

<div class=MsoNormal align=center style='text-align:center'><font size=3
face="Times New Roman"><span lang=EN-US style='font-size:12.0pt'>

<hr size=2 width="100%" align=center tabindex=-1>

</span></font></div>

<p class=MsoNormal><b><font size=2 face=Tahoma><span lang=EN-US
style='font-size:10.0pt;font-family:Tahoma;font-weight:bold'>From:</span></font></b><font
size=2 face=Tahoma><span lang=EN-US style='font-size:10.0pt;font-family:Tahoma'>
glasgow-haskell-users-bounces@haskell.org
[mailto:glasgow-haskell-users-bounces@haskell.org] <b><span style='font-weight:
bold'>On Behalf Of </span></b>Rich Neswold<br>
<b><span style='font-weight:bold'>Sent:</span></b> 13 September 2005 16:45<br>
<b><span style='font-weight:bold'>To:</span></b> Felix Breuer<br>
<b><span style='font-weight:bold'>Cc:</span></b>
glasgow-haskell-users@haskell.org<br>
<b><span style='font-weight:bold'>Subject:</span></b> Re: FFI: calling Haskell
from C++?</span></font><span lang=EN-US><o:p></o:p></span></p>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>On 9/13/05, <b><span style='font-weight:bold'>Felix Breuer</span></b>
&lt;<a href="mailto:felix@fbreuer.de">felix@fbreuer.de</a>&gt; wrote:<o:p></o:p></span></font></p>

<div>

<blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;
margin-left:4.8pt;margin-right:0cm'>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>&nbsp; $ ghc -fffi Foo.o Foo_stub.o main.cpp<br>
&nbsp;&nbsp;main.o(.text+0x22): In function `main':<br>
&nbsp;&nbsp;main.cpp: undefined reference to `__stginit_Foo()'<br>
&nbsp;&nbsp;main.o(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'<br>
&nbsp;&nbsp;collect2: ld returned 1 exit status <o:p></o:p></span></font></p>

</blockquote>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><br>
In main.cpp, you need to define __stginit_Foo(void) like this:<br>
<br>
&nbsp;&nbsp;&nbsp; extern &quot;C&quot; void __stginit_Foo(void);<br clear=all>
<br>
Specifying the &quot;C&quot; linkage with prevent the compiler from looking for
a name-mangled version of the function.<br>
<br>
To fix the second problem, you need to shut off exception handling (specifying
the -fno-exceptions option ought to do this. I don't know how to pass compiler
option via ghc, though.<br>
<br>
-- <br>
Rich<br>
<br>
AIM : rnezzy<br>
ICQ : 174908475 <o:p></o:p></span></font></p>

</div>

</div>

</body>

</html>