Reputation: 5050
I have some C functions from a dynamic library (just zlibVersion
here) that I'd like to call from bundled C code in a cabal package. Here I call it both directly as a foreign import and indirectly through the bundled C code to show that the latter crashes ghci, even after the former succeeds.
Main.hs:
module Main (main) where
import Foreign.C.String (CString, peekCString)
foreign import ccall "foreign_test" foreignTest :: IO ()
foreign import ccall "zlibVersion" zlibVersion :: IO CString
main :: IO ()
main = do
zlibVersion >>= peekCString >>= putStrLn
foreignTest
foreign_test.c:
#include <stdio.h>
#include <zlib.h>
void foreign_test() {
puts(zlibVersion());
}
It works fine from a compiled binary:
$ cabal build
$ dist/build/cabal-extra-libs-test/cabal-extra-libs-test
1.2.5
1.2.5
But the indirect call crashes ghci:
$ cabal repl
Preprocessing executable 'cabal-extra-libs-test' for
cabal-extra-libs-test-0.1.0.0...
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
1.2.5
dyld: lazy symbol binding failed: Symbol not found: _zlibVersion
Referenced from: /var/folders/pz/920gzhqn01q8d6vjkvy1yvdr0000gn/T/ghc6850_0/libghc_1.dylib
Expected in: flat namespace
dyld: Symbol not found: _zlibVersion
Referenced from: /var/folders/pz/920gzhqn01q8d6vjkvy1yvdr0000gn/T/ghc6850_0/libghc_1.dylib
Expected in: flat namespace
cabal-extra-libs-test.cabal:
name: cabal-extra-libs-test
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
executable cabal-extra-libs-test
default-language: Haskell2010
main-is: Main.hs
build-depends: base
c-sources: foreign_test.c
extra-libraries: z
GHC is 7.10.3 with cabal-install 1.24.0.0 on OS X 10.11.5. Building with stack and stack ghci produces the same crash.
Upvotes: 2
Views: 156
Reputation: 2697
That's peculiar.. I don't know much about OSX but dyld: lazy symbol binding failed: Symbol not found: _zlibVersion
gives the impression It thinks OSX is an underscore platform (e.g. one where C functions are mangled with an _).
Is this a standard GHC build? and can you try with 8.0.1
? lots has changed in the linker.
Upvotes: 1