简体   繁体   English

将 Haskell 程序编译为 C

[英]Compile Haskell programs to C

I have to following Haskell program I'm trying to compile to C. I've looked at this SO post , but could not get an answer there.我必须遵循我试图编译为 C 的 Haskell 程序。我看过这个 SO 帖子,但在那里找不到答案。

quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
  where
    lesser  = filter (<  p) xs
    greater = filter (>= p) xs

main = print(quicksort([5,2,1,0,8,3]))

Here's what I tried:这是我尝试过的:

$ ghc -C main.hs

And what I get is:我得到的是:

ghc: the option -C is only available with an unregisterised GHC
Usage: For basic information, try the `--help' option.

This is a bit weird because when I look at the help I see this:这有点奇怪,因为当我查看帮助时,我看到了:

-C stop after generating C (.hc output) -C 生成 C 后停止(.hc 输出)

Compiling to C is now a special-purpose trick, used primarily for bootstrapping on new architectures.编译为 C 现在是一种特殊用途的技巧,主要用于引导新架构。 Consequently by default it is not supported.因此,默认情况下不支持它。 The GHC wiki has some instructions for building GHC yourself with this support enabled; GHC wiki有一些说明,用于在启用此支持的情况下自己构建 GHC; the chief difference between a standard build and a build that enables compiling to C is to configure with the --enable-unregisterised flag.标准构建和能够编译为 C 的构建之间的主要区别是使用--enable-unregisterised标志进行配置。 See also the full list of pages on building GHC -- it is quite complicated, so you'll want to keep this handy if you decide to do so.另请参阅有关构建 GHC 的完整页面列表——它非常复杂,因此如果您决定这样做,您需要将其放在手边。

That option is ancient.这个选项是古老的。

Serveral years ago, GHC used to compile via C, but no longer does that in normal scenarios.几年前,GHC 曾经通过 C 编译,但在正常情况下不再这样做。 Instead of generating C code and compiling that with gcc , nowadays GHC uses its own native code generator (or LLVM).如今,GHC 不再使用生成 C 代码并使用gcc进行编译,而是使用自己的本机代码生成器(或 LLVM)。

Technically, it is possible to compile GHC itself as "unregisterised" to re-enable that option.从技术上讲,可以将 GHC 本身编译为“未注册”以重新启用该选项。 This requires a custom build of GHC from its source code.这需要从其源代码自定义构建 GHC。 This will however produce a rather inefficient C code.然而,这将产生相当低效的 C 代码。 Pragmatically, this is only done when cross-compiling or when porting GHC to a new architecture.务实地说,这只在交叉编译或将 GHC 移植到新架构时才会这样做。

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

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