title: GHCi runtime linker issue when using FFI declarations
body: <p>I have a problem regarding FFI in Haskell and the interactive mode of GHC <a href="https://stackoverflow.com/questions/10123040/ghci-doesnt-work-with-ffi-export-declarations-shared-libaries">again</a>.</p>
  
  <p>Consider <code>FFISo.hs</code>:</p>
  
  <pre><code>{-# LANGUAGE OverloadedStrings #-}
  module Main where
  
  import qualified Data.ByteString.Char8 as B
  
  import FFIFun.Foo
  
  main :: IO ()
  main = do
    B.putStrLn "main"
    callMeFromC
    callMeFromHaskell
    return ()
  </code></pre>
  
  <p><code>c.c</code>:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  void callMeFromC(void);
  
  void callMeFromHaskell(void)
  {
      printf("callMeFromHaskell\n");
      callMeFromC();
  }
  </code></pre>
  
  <p><code>FFIFun/Foo.hs</code>:</p>
  
  <pre><code>{-# LANGUAGE OverloadedStrings #-}
  {-# LANGUAGE ForeignFunctionInterface #-}
  module FFIFun.Foo where
  
  import qualified Data.ByteString.Char8 as B
  
  foreign import ccall "callMeFromHaskell"
    callMeFromHaskell :: IO ()
  
  foreign export ccall callMeFromC :: IO ()
  callMeFromC :: IO ()
  callMeFromC = B.putStrLn "callMeFromC"
  </code></pre>
  
  <p>and a <code>Makefile</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>SHELL := bash
  
  GHC_OPT := -Wall -O2 -fno-warn-unused-do-bind
  
  
  all: ffiso
  
  test: ffiso
      ./$&lt;
  
  ffiso: FFISo.hs c.c
      ghc --make $(GHC_OPT) $^ -o $@
  
  clean:
      rm -rf *{.hi,o,_stub.*} ffiso FFIFun/*{.hi,.o,_stub.*}
  
  ghci: ffiso
      ghci -package bytestring FFIFun/Foo.o c.o FFISo.hs
  </code></pre>
  
  <p>you find it also <a href="https://gist.github.com/2724649" rel="nofollow noreferrer">here as a gist</a>.</p>
  
  <p>So, my problem now:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ make ghci
  [...]
  Ok, modules loaded: Main, FFIFun.Foo.
  Prelude Main&gt; -- fine, it's loading.
  Prelude Main&gt; :t callMeFromC
  
  &lt;interactive&gt;:1:1: Not in scope: `callMeFromC'
  Prelude Main&gt; -- uhm, why?
  Prelude Main&gt; :t main
  main :: IO ()
  Prelude Main&gt; main
  
  
  GHCi runtime linker: fatal error: I found a duplicate definition for symbol
     FFIFunziFoo_callMeFromC_info
  whilst processing object file
     ./FFIFun/Foo.o
  This could be caused by:
     * Loading two different object files which export the same symbol
     * Specifying the same object file twice on the GHCi command line
     * An incorrect `package.conf' entry, causing some object to be
       loaded twice.
  GHCi cannot safely continue in this situation.  Exiting now.  Sorry.
  </code></pre>
  
  <p>Hrmpf, what is wrong here? Interestingly I get an different error on <code>i686</code> (above, it's a <code>x86_64</code> system, but both GHC 7.4.1):</p>
  
  <pre class="lang-none prettyprint-override"><code>GHCi runtime linker: fatal error: I found a duplicate definition for symbol
     __stginit_FFIFunziFoo
  whilst processing object file
     ./FFIFun/Foo.o
  This could be caused by:
     * Loading two different object files which export the same symbol
     * Specifying the same object file twice on the GHCi command line
     * An incorrect `package.conf' entry, causing some object to be
       loaded twice.
  GHCi cannot safely continue in this situation.  Exiting now.  Sorry.
  </code></pre>
  
  <p>Also, is there some documentation about it? I feel like I'm the only one who has hard times with FFI and GHCi out there.</p>
  
  <p>edit:
  note, that <code>make test</code> works fine:</p>
  
  <pre class="lang-none prettyprint-override"><code>$ ghc --make -Wall -O2 -fno-warn-unused-do-bind FFISo.hs c.c -o ffiso
  [1 of 2] Compiling FFIFun.Foo       ( FFIFun/Foo.hs, FFIFun/Foo.o )
  [2 of 2] Compiling Main             ( FFISo.hs, FFISo.o )
  Linking ffiso ...
  ./ffiso
  main
  callMeFromC
  callMeFromHaskell
  callMeFromC
  </code></pre>