[Haskell] Linker flags for foreign export.

Jason Dusek jason.dusek at gmail.com
Tue Mar 8 06:28:38 CET 2011


  I am having no luck generating a foreign export and calling it
  from C. The problem seems to be that I must spoon feed the linker.
  My working example is the files under this directory on Github:

    https://github.com/solidsnack/bash/tree/106e4de8358a726984d54859fb0b4aeaf264156f/hs/exports

  Running `make' to produce the executable that imports the
  binding yields a number of unresolved linkages:

    :;  make import
    ghc --make -fPIC -dynamic -c exports.hs
    [1 of 1] Compiling LanguageBashExports ( exports.hs, exports.o )
    ghc -shared exports.o exports_stub.o -o exports.so \
      -L/usr/lib/ghc-6.12.1/ -lHSrts \
      -L/usr/lib/ghc-6.12.1/ghc-prim-0.2.0.0/ -lHSghc-prim-0.2.0.0 \
      -L/usr/lib/ghc-6.12.1/integer-gmp-0.2.0.0/ -lHSinteger-gmp-0.2.0.0 \
      -L/usr/lib/ghc-6.12.1/base-4.2.0.0/ -lHSbase-4.2.0.0 \
      -lffi
    gcc -g -Wall -O2 -fPIC -Wall -o import \
      -I/usr/lib/ghc-6.12.1/include/ \
      import.c exports.so
    exports.so: undefined reference to `timer_settime'
    exports.so: undefined reference to `__stginit_base_DataziWord_dyn'
    exports.so: undefined reference to `clock_gettime'
    exports.so: undefined reference to `__stginit_base_Prelude_dyn'
    exports.so: undefined reference to `__stginit_base_ForeignziCziTypes_dyn'
    exports.so: undefined reference to `timer_delete'
    exports.so: undefined reference to `timer_create'
    exports.so: undefined reference to `hs_free_stable_ptr'
    collect2: ld returned 1 exit status
    make: *** [import] Error 1

  If I add linker directives for libHSrts and librt to the
  compilation of import.c, the only unresolved symbols remaining
  are the STG init functions for the Prelude, Data.Word and
  Foreign.C.Types:

    :;  make import
    gcc -g -Wall -O2 -fPIC -Wall -o import \
      -I/usr/lib/ghc-6.12.1/include/ \
      import.c exports.so \
      -L/usr/lib/ghc-6.12.1/ -lHSrts \
      -lrt
    exports.so: undefined reference to `__stginit_base_DataziWord_dyn'
    exports.so: undefined reference to `__stginit_base_Prelude_dyn'
    exports.so: undefined reference to `__stginit_base_ForeignziCziTypes_dyn'
    collect2: ld returned 1 exit status
    make: *** [import] Error 1

  This "hide and go seek" with symbols really makes me feel like
  I'm just doing it wrong, though. I linked exports.so
  statically so why shouldn't it include `hs_free_stable_ptr'
  already (as well as the stginit functions)? Not sure why I
  need to mention librt at this step and not at the previous
  one. The truth is, I don't write C very often and in fact I am
  only doing this to create a bridge to Ruby.

  How should I be approaching this? Is there an "SO-chasing"
  option?

--
Jason Dusek
Linux User #510144 | http://counter.li.org/



More information about the Haskell mailing list