這可能聽起來像一場噩夢,但我真的想得到這個工作。我大部分都使用這個例子: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 –