[Haskell-cafe] ghci and dynamically linking to Objective-C objects on Mac OS X

Sean Seefried sean.seefried at gmail.com
Sun Mar 1 06:09:14 UTC 2015


Hi all,

I fear I might be unaware of something I should be.  Say I have a file
objc_util.m with the following contents:

    #include <Foundation/Foundation.h>

    void ns_log(const char *s) {
      NSString *str = [NSString stringWithUTF8String:s];
      NSLog(@"%@", str);
    }

I also have a file Main.hs with contents:

    {-# LANGUAGE ForeignFunctionInterface #-}
    module Main where

    import Foreign.C.Types
    import Foreign.C.String

    foreign import ccall "ns_log" cNSLog :: CString -> IO ()

    nsLog s = withCString s cNSLog

I compile objc_util.m with:

    gcc -c objc_util.m

And then I try to open it up in GHCi.

    ghci objc_util.o Main.hs -framework Foundation

I add the flag '-frame Foundation' in the hopes that symbols in objc_util.o
will get resolved
dynamically.

However I get:

    GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
    Loading package ghc-prim ... linking ... done.
    Loading package integer-gmp ... linking ... done.
    Loading package base ... linking ... done.
    Loading object (static) objc_util.o ... ghc: panic! (the 'impossible'
happened)
      (GHC version 7.8.3 for x86_64-apple-darwin):
      Loading temp shared object failed:
dlopen(/var/folders/pv/pfxcyy117v31vt6lsshtgr_w0000gn/T/ghc33546_0/ghc33546_1.dylib,
9):     Symbol not found: _OBJC_CLASS_$_NSString
      Referenced from: /var/folders/pv/pfxcyy1
 17v31vt6lsshtgr_w0000gn/T/ghc33546_0/ghc33546_1.dylib
      Expected in: flat namespace
     in
/var/folders/pv/pfxcyy117v31vt6lsshtgr_w0000gn/T/ghc33546_0/ghc33546_1.dylib

    Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

However, I have found a work around to this. If I do this:

    ln -s /System/Library/Frameworks/Foundation.framework/Foundation
Foundation.o

and then run

    ghci Foundation.o objc_util.o Main.hs

I get this:

    GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
    Loading package ghc-prim ... linking ... done.
    Loading package integer-gmp ... linking ... done.
    Loading package base ... linking ... done.
    Loading object (static) Foundation.o ... done
    Loading object (static) objc_util.o ... done
    final link ... done

Everything has worked! And I can happily run 'nsLog' from GHCi and have it
appear in the
'Console' app.

However, it doesn't seem like I should have to do this. Where am I going
wrong?

Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150301/9d579ab5/attachment.html>


More information about the Haskell-Cafe mailing list