[Haskell-cafe] Need help with very simple C++ FFI call

Carter Schonwald carter.schonwald at gmail.com
Tue Nov 19 16:21:40 UTC 2013


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. :-)

Happy hacking.

On Tuesday, November 19, 2013, Thiago Padilha wrote:

> Hi
>
> I'm trying write a ffi binding to the v8 javascript engine (C++
> library). Theres one already at https://github.com/sol/v8 but I want
> to do it again as an exercise to improve by haskell skills.
>
> I chose this library be because it will force me to deal with impure
> functions using monads. Since its not a trivial library, it will also
> improve my knowledge of writing FFI bindings and cabal packages in
> case I want to wrap more C++ libraries in the future.
>
> My first step is to port the 'hello world' example in v8 embedders
> guide(https://developers.google.com/v8/get_started) as a haskell
> binding. So I have downloaded/compiled v8 and created the following
> directory structure:
>
> src/V8.hs
> cbits/haskell-v8.cpp
> # v8 libraries
> deps/lib/libv8_base.a
> deps/lib/libv8_snapshot.a
> # v8 headers
> deps/include/
>
> Here's the contents of the haskell-v8.cpp file:
> http://pastebin.com/RfYCCxFQ (basically a copy of the v8 embedders
> example function, except that it returns a char pointer).
>
> and here's the haskell module: http://pastebin.com/fnXVbEeU
>
> I compiled a shared library exposing the c "hello" function with the
> following command:
>
>     $ g++ \
>       -shared -o libhaskellv8.so \
>       -I./deps/include \
>       -L./deps/lib \
>       ./cbits/haskell-v8.cpp \
>       -lv8_base \
>       -lv8_snapshot \
>       -lpthread \
>       -lrt
>
> And loaded into ghci with the following:
>
>     $ ghci -L. -lhaskellv8 src/V8.hs
>
> The problem is that I get a segmentation fault when I call the 'hello'
> function from ghci. To ensure the library was compiled correctly, I
> successfully called it from python:
>
>     $ python -c \
>       'from ctypes import CDLL, c_char_p; lv8=CDLL("libhaskellv8.so");
> lv8.hello.restype = c_char_p; print lv8.hello()'
>     Hello, World!
>
> I have selectively uncommented lines from the c++ file, and the
> segmentation faults starts happening when I uncomment line 14
> (HandleScope handle_scope(isolate)).
>
> What am I missing here?
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org <javascript:;>
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131119/6c34a7b7/attachment.html>


More information about the Haskell-Cafe mailing list