从c-sources调用动态库在ghci中失败

时间:2016-06-10 03:52:06

标签: macos haskell ghc ghci

我有动态库中的一些C函数(这里只是zlibVersion),我想从cabal包中的捆绑C代码调用。在这里,我将其直接称为外部导入,并间接通过捆绑的C代码来表明后者崩溃ghci,即使在前者成功之后。

Main.hs:

module Main (main) where

import Foreign.C.String (CString, peekCString)

foreign import ccall "foreign_test" foreignTest :: IO ()
foreign import ccall "zlibVersion" zlibVersion :: IO CString

main :: IO ()
main = do
  zlibVersion >>= peekCString >>= putStrLn
  foreignTest

foreign_test.c:

#include <stdio.h>
#include <zlib.h>

void foreign_test() {
    puts(zlibVersion());
}

从编译的二进制文件中可以正常工作:

$ cabal build
$ dist/build/cabal-extra-libs-test/cabal-extra-libs-test
1.2.5
1.2.5

但是间接调用崩溃了ghci:

$ cabal repl
Preprocessing executable 'cabal-extra-libs-test' for
cabal-extra-libs-test-0.1.0.0...
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
1.2.5
dyld: lazy symbol binding failed: Symbol not found: _zlibVersion
  Referenced from: /var/folders/pz/920gzhqn01q8d6vjkvy1yvdr0000gn/T/ghc6850_0/libghc_1.dylib
  Expected in: flat namespace

dyld: Symbol not found: _zlibVersion
  Referenced from: /var/folders/pz/920gzhqn01q8d6vjkvy1yvdr0000gn/T/ghc6850_0/libghc_1.dylib
  Expected in: flat namespace

小集团-EXTRA-库-test.cabal:

name:                cabal-extra-libs-test
version:             0.1.0.0
build-type:          Simple
cabal-version:       >=1.10

executable cabal-extra-libs-test
  default-language: Haskell2010
  main-is: Main.hs
  build-depends: base

  c-sources: foreign_test.c
  extra-libraries: z

在OS X 10.11.5上,GHC为7.10.3,cabal-install为1.24.0.0。使用堆栈和堆栈构建ghci会产生相同的崩溃。

1 个答案:

答案 0 :(得分:1)

这很奇怪..我对OSX知之甚少,但是dyld: lazy symbol binding failed: Symbol not found: _zlibVersion给人的印象它认为OSX是一个下划线平台(例如C函数被_修复的平台)。

这是标准的GHC构建吗?你能试试8.0.1吗?链接器中的批次发生了变化。