[Haskell-cafe] Calling Haskell from C

Auke Booij auke at tulcod.com
Fri Jan 13 18:29:56 UTC 2017


The development version of cabal has support for building dynamic objects. See:
https://github.com/haskell/cabal/blob/master/Cabal/doc/developing-packages.rst#foreign-libraries

On 13 January 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
> 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
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.


More information about the Haskell-Cafe mailing list