无法编译模块导入 Foreign.R

Cannot compile a module importing Foreign.R

这里是导入Foreign.R的最小模块(注意它甚至不使用它):

-- test.hs
{-# LANGUAGE ForeignFunctionInterface #-}

module Test where

import Foreign
import Foreign.R 

foreign export ccall testR :: Ptr (Double) -> IO ()

testR :: Ptr (Double) -> IO ()
testR result = do
  poke result $ 1

这次编译失败:

ghc -fPIC -shared  -dynamic -lHSrts-ghc7.10.3 test.hs -o test.so

它给出:

Linking test.so ...
/usr/bin/ld: test.o: réadressage R_X86_64_PC32 vers symbole « Test_testR_closure » ne peut pas être utilisé en créant un objet partagé ; recompilé avec -fPIC
/usr/bin/ld : échec de l'édition de liens finale : Mauvaise valeur
collect2: error: ld returned 1 exit status

对不起法语...英语应该是这样的:

Linking test.so ...
/usr/bin/ld: test.o: relocation R_X86_64_PC32 to symbol  « Test_testR_closure » cannot be used by creating a shared object; recompile with -fPIC
/usr/bin/ld : failure edit final link : Bad value
collect2: error: ld returned 1 exit status

去掉import Foreign.R就没有问题了。

我找到了 a fix here

在 Ubuntu,我做了:

sudo update-alternatives --install "/usr/bin/ld" "ld" "/usr/bin/ld.gold" 20

现在编译没有报错了。