2
votes

I've created test project for cabal library. And I'm trying to build simple project (with only 3 .hs files with using FFI).

HCabalTestLib.cabal file is the next:

library
  exposed-modules:     Main, Parser.Template, Parser.Data
  build-depends:       base ==4.6.*
  extensions: ForeignFunctionInterface

Then, I use next commands: runhaskell Setup.hs configure runhaskell Setup.hs build

Cabal build procedure successfull create Main_stub.h header file with my FFI declarations and libHSHCabalTestLib-0.1.0.0.a As I understand, libHSHCabalTestLib-0.1.0.0.a is static library (I use Windows).

Finally, I want to use this in simple MS Visual Studio project. I copy header file in project folder, and copy library file (libHSHCabalTestLib-0.1.0.0.a) in the same folder. In Project Properties -> Linker -> Input -> Additional Dependencies I specified my library.

After "Build" my project I've got a lot of linker error:

1>CabalStaticLibExperiment.obj : error LNK2019: unresolved external symbol _hs_init referenced in function _main
1>CabalStaticLibExperiment.obj : error LNK2019: unresolved external symbol _hs_exit referenced in function _main
1>libHSHaskellCabalLib-0.1.0.0.lib(Main.o) : error LNK2001: unresolved external symbol _base_GHCziIOziHandleziText_hGetContents1_closure
1>libHSHaskellCabalLib-0.1.0.0.lib(Main.o) : error LNK2001: unresolved external symbol _stg_upd_frame_info
...
1>libHSHaskellCabalLib-0.1.0.0.lib(Data.o) : error LNK2001: unresolved external symbol _ghczmprim_GHCziCString_unpackCStringzh_info
1>CabalStaticLibExperiment.exe : fatal error LNK1120: 56 unresolved externals

I've next question: how can I connect Haskell FFI static library with MS Visual Studio project? How can I resolve this dependencies?

1

1 Answers

1
votes

Try linking against the GHC runtime, libHSrts. It should be with your ghc installation probably under lib/ghc-<version>/rts-1.0/libHSrts* (might be in a different path on Windows than the Linux path I've cited). There are quite a few different runtime choices to link against, with threading, debugging and whatnot compiled in or not. The choice of which to use probably depends on your application and the environment.

Addendum: _base_GHCziIOziHandleziText_hGetContents1_closure is a symbol in libHSbase (conveniently noted by the prefixed "base"), and _ghczmprim_GHCziCString_unpackCStringzh_info is in libHSghc-prim, which means you'll also have to link them, or command GHC to statically link your binary. Static linking may not be available for all packages, though, so when adding a new library, don't be surprised if you need to add new input to your linker. Also, downloaded libraries obviously won't sit alongside GHC, but in your user or global cabal lib directory.