4

I want to call a Haskell function that is operating on text and returns text (actually the processing is complicated and the Haskell code is split into few modules, but this probably isn't the case).

I tried the approach described here: Communication between Java and Haskell and modified it to work with Strings.

But I'm getting the error:

 error:   initializing argument 1 of ‘void* myFunction_hs(HsPtr)’ [-fpermissive]
 extern HsPtr myFunction_hs(HsPtr a1);
              ^

The relevant code and compilation is here: in Haskell:

foreign export ccall myFunction_hs :: CString -> IO CString

in Java:

import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;

@Platform(include={"<HsFFI.h>","myModule_stub.h"})
public class MyModule {
    static { Loader.load(); }
    public static native void hs_init(int[] argc, @Cast("char***") @ByPtrPtr PointerPointer argv);
    public static native String myFunction_hs(String text);
    public static void main(String[] args) {
        hs_init(null, null);
        String s = myFunction_hs("This is some String.");
        System.out.println("Result: " + s);
    }
}

And the compilation:

$ ghc -fPIC  -c -O myModule.hs
$ javac -cp javacpp.jar MyModule.java 
$ java -jar javacpp.jar -Dcompiler.path=ghc -Dcompiler.output.prefix="-optc-O3 -Wall MyModule.o -dynamic -fPIC -shared -lstdc++ -lHSrts-ghc7.6.3 -o " -Dcompiler.linkpath.prefix2="-optl -Wl,-rpath," MyModule

Do you have any idea what goes wrong?

2
  • 1
    A C string and a Java string are already quite different beasts, but an IO CString is surely not a string at all. Commented May 13, 2014 at 10:46
  • I suspect that String in Java and CString in Haskell just do not match. You need C-style string type (perhaps byte[]), I think. Commented May 13, 2014 at 10:51

1 Answer 1

3

I managed to solve this issue.

You need to edit the myModule_stub.h file (it is automatically generated). In my case I had to change the line:

extern HsPtr myFunction_hs(HsPtr a1);

to:

extern char* myFunction_hs(const char* a1);

Of course, you should rename your myModule_stub.h into myModule.h after manual editing. Otherwise it will be overwritten by GHC, and your specific types will be lost.

Sign up to request clarification or add additional context in comments.

2 Comments

I have a wish and feature request for GHC for this kind of incoveniences hindering fast integration of Haskell into C: [ more specific types in the generated *_stub.h files ](ghc.haskell.org/trac/ghc/ticket/10505).
BTW, it'd be nice if you marked your answer as accepted. As far as I understand, that's the solution so far. (A quite important addition: And you should rename your myModule_stub.h into myModule.h after manual editing, otherwise it will be overwritten by GHC, and your specific types will be lost.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.