这可能听起来像一场噩梦,但我真的想得到这个工作。我大部分都使用这个例子:Calling C from Haskell,并试图让这个工作在Ubuntu上。创建与JAVAH .h文件后从Java调用Haskell与C之间在
package test;
public class JniTest {
public native int fib(int x);
}
这在C:
我在Java中运行这个(test_JniTest.c)
#include "test_JniTest.h"
#include "Safe_stub.h"
JNIEXPORT jint JNICALL Java_test_JniTest_fib(JNIEnv * e, jobject o, jint f)
{
return fibonacci_hs(f);
}
,然后在Haskell参考(前存根):(Safe.hs)
module Safe where
import Foreign.C.Types
fibonacci :: Int -> Int
fibonacci n = fibs !! n
where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral
foreign export ccall fibonacci_hs :: CInt -> CInt
,这就是我想要一起编译:
ghc -c -O Safe.hs
依次为:
ghc -shared -o libTest.jnilib -optc-O test_JniTest.c -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux
和我收到此错误:
/usr/bin/ld: test_JniTest.o: relocation R_X86_64_PC32 against undefined symbol `fibonacci_hs' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Bad value collect2: ld returned 1 exit status
我没有以任何方式一个C专家,不知道怎么处理这件事。我尝试用-fPIC编译各种不同的方式,但我一直在得到相同的错误。任何想法我可能做错了什么?
谢谢!
C代码不需要在某处启动Haskell运行时?我认为你需要把它放到代码的某个地方,无论是C端还是Java端。 –
您是否考虑使用JNA(https://github.com/twall/jna#readme)而不是JNI? – Landei
我已经在这里回答了这个问题:http://stackoverflow.com/questions/10370177/communication-between-java-and-haskell/10370902#10370902 –