Calling Haskell from Java with C in between

前端 未结 2 693
梦毁少年i
梦毁少年i 2021-02-07 04:06

This probably sounds like a nightmare, but I\'d really like to get this working. I am using this example for the most part: Calling C from Haskell and am trying to get this work

相关标签:
2条回答
  • 2021-02-07 04:41

    Although I've pretty much answered this question here: Communication between Java and Haskell, since this issue is more about the error itself, I will be adding the details for that here. The issue stems from Haskell not supporting shared libraries very well, while Java requires them. Buildings plugins as Haskell shared libs gives us this insight and workaround:

    In principle you can use -shared without -dynamic in the link step. That would mean to statically link the rts all the base libraries into your new shared library. This would make a very big, but standalone shared library. However that would require all the static libraries to have been built with -fPIC so that the code is suitable to include into a shared library and we don't do that at the moment.

    If we use ldd again to look at the libfoo.so that we've made we will notice that it is missing a dependency on the rts library. This is problem that we've yet to sort out, so for the moment we can just add the dependency ourselves:

    $ ghc --make -dynamic -shared -fPIC Foo.hs -o libfoo.so \
     -lHSrts-ghc6.11 -optl-Wl,-rpath,/opt/ghc/lib/ghc-6.11/
    

    This is a workaround because it requires us to know the version of the rts library at build time.

    0 讨论(0)
  • 2021-02-07 04:56

    If your goal is to actually get something done (as opposed to just playing around with JNI) I suggest tackling this as a garden variety RPC problem and utilizing one of the many framework/protocols for it:

    Protocol Buffers from Google

    Thrift from Facebook

    Avro (well this is mostly a wire protocol)

    From what you are trying to do, Thrift might be your best bet since it describes a full client/server RPC stack but I'm pretty sure any of them would pretty much work over a simple socket.

    0 讨论(0)
提交回复
热议问题