My project uses FFI, so I always have to link against precompiled object files testing with GHCi:
ghci Foo a.o
My question is: Is there a way to do this once GHCi is running instead of GHCi startup? I've tried this:
$ ghci
> :l Foo a.o
but I get the error a.o is not a module name or a source file
My goal for this is to be able to start a GHCi session with external symbols linked in via a .ghci
file. My motivation is that cabal compiles the object files into dist/build/my-tests/my-tests-tmp/src/../../../../a.o
, which ends up being 92 characters long. It's bad enough to have to type this once (with a *,o
at the end), but due to a regression in GHC 7.8, I have to link five object files in a specific order which requires over 500 characters after ghci Foo
.
** UPDATE **
The following example demonstrates why n.m.'s solution below isn't working for me on GHC 7.8.2:
mul.c
#include <inttypes.h>
void mulRq (int64_t* a, int64_t* b, int32_t totm, int64_t q) { }
Mul.hs
module Mul where
import Data.Int
import Foreign.Ptr
foreign import ccall unsafe "mulRq" mulRq ::
Ptr Int64 -> Ptr Int64 -> Int64 -> Int64 -> IO ()
Dummy.hs is empty.
Steps:
ghci Mul
[can't find label mulRq]
- gcc -c -o mul.o mul.c
ghci Mul mul.o
[ghci loads]
- ghci -fobject-code Dummy
ghci Dummy
[ghci loads without compiling]
- ld -r Dummy.o mul.o -o temp.o
- mv temp.o Dummy.o
ghci Dummy
[ghci loads without compiling]
nm Dummy.o
[verify that Dummy.o contains the symbol mulRq]
Now start ghci:
$ ghci
GHCi, version 7.8.2:
...
Prelude> :l Dummy
Ok, modules loaded: Main.
Prelude Main> :l Mul
[1 of 1] Compiling Mul ( Mul.hs, interpreted )
ByteCodeLink: can't find label
During interactive linking, GHCi couldn't find the following symbol:
mulRq
.ghci
? – Ry-♦.ghci
file on a per-project basis that you can set up to load the object files you need just for that project. It'll load them all when you start GHCi, but at least you can control it more for whatever project you're working on and don't have to have them global. – bheklilr