问题
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