我尝试使用ghc编译和链接简单的程序,但在链接期间失败:
import System (getArgs)
main = do
args <- getArgs
print args
我试着用
编译% ghc -c -O Main.hs
% ghc -o Main Main.o
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog
Undefined symbols for architecture i386:
"___stginit_haskell98zm1zi1zi0zi1_System_", referenced from:
___stginit_Main_ in Main.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
zsh: exit 1 ghc -o Main Main.o
但是,当使用——make:
编译时,% ghc --make Main.hs
一切正常(除了大量的旧警告)
关于环境的更多信息:
% ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.0.3
From Haskell Platform for Mac OS X 10.6 (Intel, 32位GHC)
系统:Max OS X Lion 10.7.2
你知道怎么回事吗?
(顺便说一句,我试图安装HP x64,但在安装过程中失败)
Michael历来是正确的。有了--make
, ghc会自己找出要使用哪个包并链接进去(除非两个安装的包暴露了相同的模块名,否则它无法找出要使用哪个),没有--make
,你必须告诉它。但是,在7.0版本中,--make
是ghc的默认模式,因此普通的ghc Main.hs
现在与ghc --make Main.hs
相同。这里的区别在于两步编译。我不知道具体的细节,但原因是模块System
在haskell98包中(建议,请使用分层模块,getArgs应该通过System导入。环境,从7.2开始,haskell98不能与base)一起使用,默认情况下base没有链接。所以ghc -o Main Main.o
在默认包中找不到符号。您必须显式地告诉它查找haskell98包,ghc -c -O Main.hs; ghc -package haskell98 -o Main Main.o
应该可以工作(并且它在这里工作,我已经在7.0.4中进行了测试以确保)。
可能是因为您正在使用System中的某些东西?ghc --make
可能会自动检测需要链接的Haskell库,而ghc
本身不会。