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 -&gt; BS.ByteString<br>rev bstr = BS.reverse bstr <br> <br>rev_hs :: CString -&gt; IO CString<br>
rev_hs cstr = <br>    do { bstr &lt;- BS.packCString cstr<br>       ; let bstr&#39; = rev bstr<br>       ; cstr&#39; &lt;- newCString (show bstr&#39;)<br>       ; return cstr&#39;<br>       }<br> <br>foreign export ccall rev_hs :: CString -&gt; IO CString<br>
<br><br>And here is the C counter-part.<br><br>#include &quot;B_stub.h&quot;<br>#include &lt;stdio.h&gt;<br> <br>int main(int argc, char *argv[]) {<br>  char *str;<br>  hs_init(&amp;argc, &amp;argv);<br>  <br>  str = rev_hs(&quot;it works.&quot;);<br>
  printf(&quot;Rev: %s\n&quot;, 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>  &quot;___stginit_bytestringzm0zi9zi1zi4_DataziByteString_&quot;, referenced from:<br>      ___stginit_Lib_ in B.o<br>  &quot;_bytestringzm0zi9zi1zi4_DataziByteString_zdwreverse_info&quot;, referenced from:<br>
      _s19w_info in B.o<br>  &quot;_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_info&quot;, referenced from:<br>      _s19v_info in B.o<br>  &quot;_bytestringzm0zi9zi1zi4_DataziByteStringziInternal_zdwshowsPrec_closure&quot;, referenced from:<br>
      _Lib_zdwa_srt in B.o<br>  &quot;_bytestringzm0zi9zi1zi4_DataziByteString_zdwa4_info&quot;, referenced from:<br>      _Lib_zdwa_info in B.o<br>  &quot;_bytestringzm0zi9zi1zi4_DataziByteString_reverse_info&quot;, 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>