简体   繁体   English

为什么Haskell ghc不起作用,但是runghc运行良好?

[英]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. 一个代码工作来自runghc,但我无法使用ghc命令编译相同的代码。 Why? 为什么?

Below is my minimal code and environment: https://gist.github.com/1588756 以下是我的最小代码和环境: 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 Macbook air,max os x雪豹

The .cs extension shown in your paste is wrong; 粘贴中显示的.cs扩展名是错误的; 1 rename the file to cat.hs and it'll work fine. 1将文件重命名为cat.hs ,它将正常工作。

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; 传递文件时发生GHC不知道如何处理; 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... 1至少在GHC获得C#支持之前......

With the filename cat.cs , on linux I get 使用文件名cat.cs ,在Linux上我得到

$ 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. 所以确实,因为GHC不知道如何处理.cs文件,它将它们原样传递给gcc进行链接,gcc也不知道,所以回过头来看它作为链接器脚本,当然不会结果很好。

But you can tell GHC that it should treat whatever file you give it as, say a .hs file, 但你可以告诉GHC它应该处理你给它的任何文件,例如.hs文件,

$ 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. 另一方面, runghc并不关心文件的名称,它会尝试将文件解释为普通的Haskell源,除非它具有扩展名.lhs ,然后它会尝试将其解释为有文化的Haskell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM