[Haskell-cafe] Calling Haskell from C

Dominic Steinitz dominic at steinitz.org
Fri Jan 13 10:59:40 UTC 2017


I've been trying to get the example in
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ffi-chap.html?highlight=foreign#using-foreign-export-and-foreign-import-ccall-wrapper-with-ghc
working.

I have a cabal file

name:                test-via-c
version:             0.1.0.0
homepage:            TBD
license:             MIT
author:              Dominic Steinitz
maintainer:          idontgetoutmuch at gmail.com
category:            System
build-type:          Simple
cabal-version:       >=1.10

executable Foo.dylib
  main-is: Foo.hs
  other-extensions:    ForeignFunctionInterface
  build-depends:       base >=4.7 && <4.10,
                       primitive >=0.6 && <0.7
  hs-source-dirs:      src
  default-language:    Haskell2010
  include-dirs:        src
  ghc-options:         -O2 -shared -fPIC -dynamic
  extra-libraries:     HSrts-ghc8.0.1

which installs

/Users/dom/Library/Haskell/ghc-8.0.1/lib/test-via-c-0.1.0.0/bin/Foo.dylib

I then compile the C with

gcc-6 Bar.c
/Users/dom/Library/Haskell/ghc-8.0.1/lib/test-via-c-0.1.0.0/bin/Foo.dylib
-I/Library/Frameworks/GHC.framework/Versions/8.0.1-x86_64/usr/lib/ghc-8.0.1/include
-L/Library/Frameworks/GHC.framework/Versions/8.0.1-x86_64/usr/lib/ghc-8.0.1/rts
-lHSrts-ghc8.0.1

Sadly when I try to run the executable I get

~/Dropbox/Private/TestU01-1.2.3/examples/src $ ./a.out
dyld: Library not loaded: @rpath/Foo.dylib
  Referenced from: /Users/dom/Dropbox/Private/TestU01-1.2.3/examples/src/./a.out
  Reason: image not found
Trace/BPT trap: 5

I am guessing the executable is looking for the dynamic library. I
copied Foo.dylib to the current directory but still get the same
error.

I know this is really an OS X question but I am hoping someone who has
done before will be able to help.

Dominic Steinitz
dominic at steinitz.org
http://idontgetoutmuch.wordpress.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170113/aad256e5/attachment.html>


More information about the Haskell-Cafe mailing list