带有 Data.Set 的 Haskell 程序无法编译

发布于 2024-10-01 14:38:23 字数 1580 浏览 1 评论 0 原文

我编写以下文件 temp.hs:

import qualified Data.Set
import System.Environment

main :: IO ()
main = do
        args <- getArgs
        let fname = head args
        print (fname)

它在 ghci 中加载,没有错误:

$ ghci
GHCi, version 6.12.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 package ffi-1.0 ... linking ... done.
Prelude> :load temp.hs 
[1 of 1] Compiling Main             ( temp.hs, interpreted )
Ok, modules loaded: Main.
*Main> 

当我尝试编译它时,出现以下错误:

$ ghc temp.hs -o temp
Undefined symbols:
  "___stginit_containerszm0zi3zi0zi0_DataziSet_", referenced from:
      ___stginit_Main_ in temp.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

如果我取出 import Data.Set,它可以正常编译。

版本信息:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.3

$ gcc --ver
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc_40/gcc_40-5494~112/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin10 --with-arch=apple --with-tune=generic --host=i686-apple-darwin10 --target=i686-apple-darwin10
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5494)

I write the following file temp.hs:

import qualified Data.Set
import System.Environment

main :: IO ()
main = do
        args <- getArgs
        let fname = head args
        print (fname)

It loads in ghci without errors:

$ ghci
GHCi, version 6.12.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 package ffi-1.0 ... linking ... done.
Prelude> :load temp.hs 
[1 of 1] Compiling Main             ( temp.hs, interpreted )
Ok, modules loaded: Main.
*Main> 

When I try to compile it, I get the following errors:

$ ghc temp.hs -o temp
Undefined symbols:
  "___stginit_containerszm0zi3zi0zi0_DataziSet_", referenced from:
      ___stginit_Main_ in temp.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

If I take the import Data.Set out, it compiles fine.

Version Info:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.3

$ gcc --ver
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc_40/gcc_40-5494~112/src/configure --disable-checking -enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib --build=i686-apple-darwin10 --with-arch=apple --with-tune=generic --host=i686-apple-darwin10 --target=i686-apple-darwin10
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5494)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2024-10-08 14:38:23

您可以单独添加软件包(正如 KennyTM 建议的那样),或者您可以只使用 --make,这在很多方面都更加方便:

ghc --make temp.hs -o temp

这将导致 GHC 在其已安装的软件包中查找任何所需的模块。与 GHCi 的做法相同。

You can add packages individually (as KennyTM suggests), or you can just use --make, which is much more convenient in a number of ways:

ghc --make temp.hs -o temp

This will cause GHC to look for any required modules among its installed packages in exactly the same way that GHCi does.

无人接听 2024-10-08 14:38:23
ghc -package containers temp.hs -o temp

当你实际使用该库时,你可以通过 ghci 检查可能需要哪个包:

  GHCi, version 6.12.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 package ffi-1.0 ... linking ... done.
  Ok, modules loaded: Main.
  Prelude Main> Data.Set.singleton 0
> Loading package array-0.3.0.1 ... linking ... done.
> Loading package containers-0.3.0.0 ... linking ... done.
  fromList [0]
  Prelude Main> 
ghc -package containers temp.hs -o temp

You can check which package may be needed with ghci, when you actually use the library:

  GHCi, version 6.12.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 package ffi-1.0 ... linking ... done.
  Ok, modules loaded: Main.
  Prelude Main> Data.Set.singleton 0
> Loading package array-0.3.0.1 ... linking ... done.
> Loading package containers-0.3.0.0 ... linking ... done.
  fromList [0]
  Prelude Main> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文