Compile Haskell programs to C

☆樱花仙子☆ 提交于 2019-12-22 05:14:38

问题


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.

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)


回答1:


Compiling to C is now a special-purpose trick, used primarily for bootstrapping on new architectures. Consequently by default it is not supported. The GHC wiki has some instructions for building GHC yourself with this support enabled; the chief difference between a standard build and a build that enables compiling to C is to configure with the --enable-unregisterised flag. 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.




回答2:


That option is ancient.

Serveral years ago, GHC used to compile via C, but no longer does that in normal scenarios. Instead of generating C code and compiling that with gcc, nowadays GHC uses its own native code generator (or LLVM).

Technically, it is possible to compile GHC itself as "unregistered" to re-enable that option. This requires a custom build of GHC from its source code. This will however produce a rather inefficient C code. Pragmatically, this is only done when cross-compiling or when porting GHC to a new architecture.



来源:https://stackoverflow.com/questions/52037888/compile-haskell-programs-to-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!