可能重複:
Small Haskell program compiled with GHC into huge binary爲什麼Haskell/GHC可執行文件在文件大小上如此之大?
最近我注意到大Haskell的可執行文件是如何。下面的所有內容都是在GHC 7.4.1上編譯的,在Linux上是-O2
。
Hello World(
main = putStrLn "Hello World!"
)超過800 KiB。在其上運行strip
可將文件大小減少到500 KiB;即使向編譯添加-dynamic
也沒有什麼幫助,留給我一個大約400 KiB的可執行文件。編譯一個涉及Parsec的非常原始的例子會產生一個1.7 MiB文件。
-- File: test.hs import qualified Text.ParserCombinators.Parsec as P import Data.Either (either) -- Parses a string of type "x y" to the tuple (x,y). testParser :: P.Parser (Char, Char) testParser = do a <- P.anyChar P.char ' ' b <- P.anyChar return (a, b) -- Parse, print result. str = "1 2" main = print $ either (error . show) id . P.parse testParser "" $ str -- Output: ('1','2')
秒差距可以是更大的庫,但我只使用它的一個微小的子集,並且確實通過上述生成的優化核心代碼是比可執行顯着更小:
$ ghc -O2 -ddump-simpl -fforce-recomp test.hs | wc -c 49190 (bytes)
因此,在程序中實際找到大量的Parsec並不是這種情況,這是我最初的假設。
爲什麼這麼龐大的可執行文件?有什麼我可以做的(除了動態鏈接)?
@DanielWagner另一個問題當然是相關的,但即使使用了描述的技術,Hello World仍然很大。另外:爲什麼編譯時應該包含整個程序的小核心代碼變得如此之大? – David
有一個相當大的運行時系統。 – augustss
@大衛:核心不包含整個程序,除非一切都被內聯了,這是不太可能的。所以它會在Parsec中鏈接,除非你用'-split-objs'構建它(見[相關答案](http://stackoverflow.com/a/9198223/98117)),它必須鏈接到所有的。 – hammar