It works indeed. Thanks.<br><br>-Kenny<br><br><div class="gmail_quote">On Fri, Jun 12, 2009 at 3:50 PM, Jochem Berndsen <span dir="ltr">&lt;<a href="mailto:jochem@functor.nl">jochem@functor.nl</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">kenny lu wrote:<br>
&gt; Hi,<br>
&gt;<br>
&gt; I was trying to write a FFI wrapper for my Haskell program which manipulates<br>
&gt;<br>
&gt; ByteString. But I am unable to compile/link it.<br>
&gt;<br>
&gt;<br>
&gt; Here is the toy program.<br>
&gt;<br>
&gt; {-# LANGUAGE ForeignFunctionInterface #-}<br>
&gt;<br>
&gt; module B where<br>
&gt;<br>
&gt; import Foreign.C.Types<br>
&gt; import Foreign.C.String<br>
&gt; import qualified Data.ByteString as BS<br>
&gt;<br>
&gt; rev :: BS.ByteString -&gt; BS.ByteString<br>
&gt; rev bstr = BS.reverse bstr<br>
&gt;<br>
&gt; rev_hs :: CString -&gt; IO CString<br>
&gt; rev_hs cstr =<br>
&gt;     do { bstr &lt;- BS.packCString cstr<br>
&gt;        ; let bstr&#39; = rev bstr<br>
&gt;        ; cstr&#39; &lt;- newCString (show bstr&#39;)<br>
&gt;        ; return cstr&#39;<br>
&gt;        }<br>
&gt;<br>
&gt; foreign export ccall rev_hs :: CString -&gt; IO CString<br>
&gt;<br>
&gt;<br>
&gt; And here is the C counter-part.<br>
&gt;<br>
&gt; #include &quot;B_stub.h&quot;<br>
&gt; #include &lt;stdio.h&gt;<br>
&gt;<br>
&gt; int main(int argc, char *argv[]) {<br>
&gt;   char *str;<br>
&gt;   hs_init(&amp;argc, &amp;argv);<br>
&gt;<br>
&gt;   str = rev_hs(&quot;it works.&quot;);<br>
&gt;   printf(&quot;Rev: %s\n&quot;, str);<br>
&gt;<br>
&gt;   hs_exit();<br>
&gt;   return 0;<br>
&gt; }<br>
&gt;<br>
&gt; Compiling B.hs alone seems fine, but errors popped up when I was trying to<br>
&gt; compile/link it with C.<br>
&gt;<br>
&gt; $ ghc -c -O B.hs<br>
&gt;<br>
&gt; $ ghc -optc-O test_b.c B.o B_stub.o -o test_b<br>
&gt; Undefined symbols:<br>
&gt;   &quot;___stginit_bytestringzm0zi9zi1zi4_DataziByteString_&quot;, referenced from:<br>
&gt;       ___stginit_Lib_ in B.o<br>
&gt;   &quot;_bytestringzm0zi9zi1zi4_DataziByteString_zdwreverse_info&quot;, referenced<br>
&gt; from:<br>
&gt;       _s19w_info in B.o<br>
&gt;   &quot;_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_info&quot;,<br>
&gt; referenced from:<br>
&gt;       _s19v_info in B.o<br>
&gt;   &quot;_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_closure&quot;,<br>
&gt; referenced from:<br>
&gt;       _Lib_zdwa_srt in B.o<br>
&gt;   &quot;_bytestringzm0zi9zi1zi4_DataziByteString_zdwa4_info&quot;, referenced from:<br>
&gt;       _Lib_zdwa_info in B.o<br>
&gt;   &quot;_bytestringzm0zi9zi1zi4_DataziByteString_reverse_info&quot;, referenced from:<br>
&gt;       _Lib_rev_info in B.o<br>
&gt; ld: symbol(s) not found<br>
&gt; collect2: ld returned 1 exit status<br>
&gt;<br>
&gt;<br>
&gt; If I replace ByteString with the ordinary String, the above programs can be<br>
&gt; compiled and linked.<br>
&gt;<br>
&gt; Can someone tell me what I did wrong here?<br>
<br>
</div></div>Add -package bytestring to the ghc command line options. I believe that<br>
adding --make also may work.<br>
<br>
Regards,<br>
<font color="#888888">--<br>
Jochem Berndsen | <a href="mailto:jochem@functor.nl">jochem@functor.nl</a><br>
GPG: 0xE6FABFAB<br>
</font></blockquote></div><br>