[Haskell-cafe] Calling Haskell from C

dominic at steinitz.org dominic at steinitz.org
Fri Jan 13 13:53:32 UTC 2017


Answering my own question:

> export DYLD_LIBRARY_PATH=/Users/dom/Library/Haskell/ghc-8.0.1/lib/test-via-c-0.1.0.0/bin:/Library/Frameworks/GHC.framework/Versions/8.0.1-x86_64/usr/lib/ghc-8.0.1/rts


> ./a.out 
> 2500
> 2500
> 2500
> 2500
> 2500

> On 13 Jan 2017, at 10:59, Dominic Steinitz <dominic at steinitz.org> wrote:
> 
> 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 <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 <mailto: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 <mailto:dominic at steinitz.org>
> http://idontgetoutmuch.wordpress.com <http://idontgetoutmuch.wordpress.com/>

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/31f27570/attachment.html>


More information about the Haskell-Cafe mailing list