Hi,<br><br>I was trying to write a FFI wrapper for my Haskell program which manipulates <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 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 from:<br>
_s19w_info in B.o<br> "_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_info", referenced from:<br> _s19v_info in B.o<br> "_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_closure", 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 compiled and linked.<br><br>Can someone tell me what I did wrong here?<br>
<br>-Kenny<br><br><br>