Why Haskell ghc does not work, but runghc works well?

One code work from runghc, but I can not compile same one with ghc command. Why?

Below is my minimal code and environment: https://gist.github.com/1588756

Works well:

$ runghc cat.hs

Can not compile:

$ ghc cat.hs -o cat

Macbook air, max os x snow leopard


The .cs extension shown in your paste is wrong;1 rename the file to cat.hs and it'll work fine.

This error message:

ld: warning: ignoring file cat.cs, file was built for unsupported file format
which is not the architecture being linked (i386)

occurs when you pass a file GHC doesn't know how to handle; it just passes it on directly to the linker, which then ignores it as it doesn't know, either. :)

1 At least until GHC gets C# support...


With the filename cat.cs , on linux I get

$ ghc cat.cs
/usr/lib64/gcc/x86_64-suse-linux/4.5/../../../../x86_64-suse-linux/bin/ld:cat.cs: file format not recognized; treating as linker script
/usr/lib64/gcc/x86_64-suse-linux/4.5/../../../../x86_64-suse-linux/bin/ld:cat.cs:1: syntax error

So indeed, since GHC doesn't know how to handle .cs files, it passes them as is to gcc for linking, gcc doesn't know either, so falls back to regarding it as a linker script, which of course doesn't turn out so well.

But you can tell GHC that it should treat whatever file you give it as, say a .hs file,

$ ghc -x hs cat.cs
[1 of 1] Compiling Main             ( cat.cs, cat.o )
Linking cat ...

runghc on the other hand doesn't care what the file is named, it tries to interpret the file as normal Haskell source, except if it has the extension .lhs , then it tries to interpret it as literate Haskell.

链接地址: http://www.djcxy.com/p/43172.html

上一篇: Haskell Thrift库在性能测试中比C ++慢300倍

下一篇: 为什么Haskell ghc不起作用,但runghc运作良好?