[Haskell-cafe] Need help with very simple C++ FFI call
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. :-)
On Tuesday, November 19, 2013, Thiago Padilha wrote:
> 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:
> # v8 libraries
> # v8 headers
> 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 \
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe