我想调用一个
Haskell函数,该函数在文本上运行并返回文本(实际上处理很复杂,Haskell代码被分成几个模块,但事实并非如此).
我尝试了这里描述的方法:Communication between Java and Haskell并修改它以使用字符串.
但是我收到了错误:
error: initializing argument 1 of ‘void* myFunction_hs(HsPtr)’ [-fpermissive]
extern HsPtr myFunction_hs(HsPtr a1);
^
相关代码和编译在这里:
在哈斯克尔:
foreign export ccall myFunction_hs :: CString -> IO CString
在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);
}
}
并编译:
$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
你知道出了什么问题吗?
最佳答案 我设法解决了这个问题.
您需要编辑myModule_stub.h文件(它是自动生成的).
在我的情况下,我不得不改变这条线:
extern HsPtr myFunction_hs(HsPtr a1);
至:
extern char* myFunction_hs(const char* a1);
当然,您应该在手动编辑后将myModule_stub.h重命名为myModule.h.否则它将被GHC覆盖,您的特定类型将丢失.