If you're wanting to call c++ code from ghci, you may need to use GHC 7.8 (current head), at least if you've hit any linking problems with using ghci in 7.6 or older ghc's.  Just an advanced warning. :-)<div><br>
</div><div>Happy hacking. <span></span><br><br>On Tuesday, November 19, 2013, Thiago Padilha  wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi<br>
<br>
I'm trying write a ffi binding to the v8 javascript engine (C++<br>
library). Theres one already at <a href="https://github.com/sol/v8" target="_blank">https://github.com/sol/v8</a> but I want<br>
to do it again as an exercise to improve by haskell skills.<br>
<br>
I chose this library be because it will force me to deal with impure<br>
functions using monads. Since its not a trivial library, it will also<br>
improve my knowledge of writing FFI bindings and cabal packages in<br>
case I want to wrap more C++ libraries in the future.<br>
<br>
My first step is to port the 'hello world' example in v8 embedders<br>
guide(<a href="https://developers.google.com/v8/get_started" target="_blank">https://developers.google.com/v8/get_started</a>) as a haskell<br>
binding. So I have downloaded/compiled v8 and created the following<br>
directory structure:<br>
<br>
src/V8.hs<br>
cbits/haskell-v8.cpp<br>
# v8 libraries<br>
deps/lib/libv8_base.a<br>
deps/lib/libv8_snapshot.a<br>
# v8 headers<br>
deps/include/<br>
<br>
Here's the contents of the haskell-v8.cpp file:<br>
<a href="http://pastebin.com/RfYCCxFQ" target="_blank">http://pastebin.com/RfYCCxFQ</a> (basically a copy of the v8 embedders<br>
example function, except that it returns a char pointer).<br>
<br>
and here's the haskell module: <a href="http://pastebin.com/fnXVbEeU" target="_blank">http://pastebin.com/fnXVbEeU</a><br>
<br>
I compiled a shared library exposing the c "hello" function with the<br>
following command:<br>
<br>
    $ g++ \<br>
      -shared -o libhaskellv8.so \<br>
      -I./deps/include \<br>
      -L./deps/lib \<br>
      ./cbits/haskell-v8.cpp \<br>
      -lv8_base \<br>
      -lv8_snapshot \<br>
      -lpthread \<br>
      -lrt<br>
<br>
And loaded into ghci with the following:<br>
<br>
    $ ghci -L. -lhaskellv8 src/V8.hs<br>
<br>
The problem is that I get a segmentation fault when I call the 'hello'<br>
function from ghci. To ensure the library was compiled correctly, I<br>
successfully called it from python:<br>
<br>
    $ python -c \<br>
      'from ctypes import CDLL, c_char_p; lv8=CDLL("libhaskellv8.so");<br>
lv8.hello.restype = c_char_p; print lv8.hello()'<br>
    Hello, World!<br>
<br>
I have selectively uncommented lines from the c++ file, and the<br>
segmentation faults starts happening when I uncomment line 14<br>
(HandleScope handle_scope(isolate)).<br>
<br>
What am I missing here?<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'Haskell-Cafe@haskell.org')">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div>