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"><<a href="mailto:jochem@functor.nl">jochem@functor.nl</a>></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>
> Hi,<br>
><br>
> I was trying to write a FFI wrapper for my Haskell program which manipulates<br>
><br>
> ByteString. But I am unable to compile/link it.<br>
><br>
><br>
> Here is the toy program.<br>
><br>
> {-# LANGUAGE ForeignFunctionInterface #-}<br>
><br>
> module B where<br>
><br>
> import Foreign.C.Types<br>
> import Foreign.C.String<br>
> import qualified Data.ByteString as BS<br>
><br>
> rev :: BS.ByteString -> BS.ByteString<br>
> rev bstr = BS.reverse bstr<br>
><br>
> rev_hs :: CString -> IO CString<br>
> rev_hs cstr =<br>
> do { bstr <- BS.packCString cstr<br>
> ; let bstr' = rev bstr<br>
> ; cstr' <- newCString (show bstr')<br>
> ; return cstr'<br>
> }<br>
><br>
> foreign export ccall rev_hs :: CString -> IO CString<br>
><br>
><br>
> And here is the C counter-part.<br>
><br>
> #include "B_stub.h"<br>
> #include <stdio.h><br>
><br>
> int main(int argc, char *argv[]) {<br>
> char *str;<br>
> hs_init(&argc, &argv);<br>
><br>
> str = rev_hs("it works.");<br>
> printf("Rev: %s\n", str);<br>
><br>
> hs_exit();<br>
> return 0;<br>
> }<br>
><br>
> Compiling B.hs alone seems fine, but errors popped up when I was trying to<br>
> compile/link it with C.<br>
><br>
> $ ghc -c -O B.hs<br>
><br>
> $ ghc -optc-O test_b.c B.o B_stub.o -o test_b<br>
> Undefined symbols:<br>
> "___stginit_bytestringzm0zi9zi1zi4_DataziByteString_", referenced from:<br>
> ___stginit_Lib_ in B.o<br>
> "_bytestringzm0zi9zi1zi4_DataziByteString_zdwreverse_info", referenced<br>
> from:<br>
> _s19w_info in B.o<br>
> "_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_info",<br>
> referenced from:<br>
> _s19v_info in B.o<br>
> "_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_closure",<br>
> referenced from:<br>
> _Lib_zdwa_srt in B.o<br>
> "_bytestringzm0zi9zi1zi4_DataziByteString_zdwa4_info", referenced from:<br>
> _Lib_zdwa_info in B.o<br>
> "_bytestringzm0zi9zi1zi4_DataziByteString_reverse_info", referenced from:<br>
> _Lib_rev_info in B.o<br>
> ld: symbol(s) not found<br>
> collect2: ld returned 1 exit status<br>
><br>
><br>
> If I replace ByteString with the ordinary String, the above programs can be<br>
> compiled and linked.<br>
><br>
> 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>