ghc 编译的二进制文件是否需要 GHC 还是它们是独立的?
如果朋友想要运行我的 Haskell 二进制文件,他是否必须先安装 Haskell,还是可以立即自行运行二进制文件?
Mac、Windows 和 Linux 上的答案相同吗?
If a friend wants to run my Haskell binaries, does he have to first install Haskell, or can he immediately run the binary by itself?
Is the answer the same on Mac, Windows, and Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
GHC 确实会生成不需要安装 GHC 本身的独立二进制文件,但是它们确实链接到一些动态库,尤其是
libgmp
。其余的库通常在大多数 Linux 系统上都是开箱即用的。我相信 Windows 上的情况也类似。您可以在 Linux 上使用
ldd
检查您所依赖的动态库。以下是我在 Ubuntu Natty 上得到的一个简单的 Hello World 程序:GHC does produce stand-alone binaries that do not require GHC itself to be installed, however they do link against some dynamic libraries, most notably
libgmp
. The remaining libraries are commonly found out of the box on most Linux systems. I believe the situation is similar on Windows.You can check which dynamic libraries you depend on using
ldd
on Linux. Here's what I get on Ubuntu Natty for a simple Hello World program:GHC 将 Haskell 编译为目标代码,并带有链接的运行时。这意味着您不需要安装 Haskell 编译器来执行 Haskell 程序。
生成的可执行文件将使用静态和动态链接的某种变体,用于 C 和 Haskell 库依赖项。任何静态链接的东西都不需要安装在用户的机器上。任何动态链接的东西都必须安装。
要查看 Linux(或 Cygwin)上需要随可执行文件一起提供的内容,请使用 ldd。您可以通过将
-static
传递给 GHC 来强制几乎所有内容的静态链接。GHC compiles Haskell to object code, with a linked runtime. That means that you do not need a Haskell compiler installed to execute Haskell programs.
The generated executable will use some variant of static and dynamic linking, for both C and Haskell library dependencies. Anything that is statically linked does not need to be installed on the user's machine. Anything that is dynamically linked must be installed.
To see what you need to ship along with the executable, on Linux (or Cygwin), use
ldd
. You can force static linking of almost everything by passing-static
to GHC.如果需要将某些 C 库与 Haskell 可执行文件静态链接,在 Linux 上,您可以将
--whole-archive
与 GNU 链接器一起使用;例如:虽然它们很常见,但
libffi
和libz
并不是普遍存在的(libffi
是我经常在 Haskell 二进制文件中看到的一个) 。这种方法是最近在 haskell-cafe 上首次向我建议的。
If it's desirable to statically link some C libraries with your Haskell executable, on Linux you can use
--whole-archive
with the GNU linker; for example:While they're quite common,
libffi
andlibz
are not ubiquitous (libffi
is one that I often see in my Haskell binaries).This approach was first suggested to me quite recently, on haskell-cafe.
大多数二进制文件不需要安装 GHC。有些(例如 xmonad)使用 Haskell 作为配置语言;在这些情况下,您将需要一个编译器。
还有一个静态链接与动态链接的问题。我相信现在的默认设置仍然是静态链接,在这种情况下,将二进制文件从一台机器迁移到另一台机器应该很容易(只需要具有相同的体系结构和操作系统)。
An installation of GHC is not required for most binaries. Some (e.g. xmonad) use Haskell as their configuration language; in those cases you will need a compiler.
There is also a question of static vs. dynamic linking. I believe the default for now is still static linking, in which case it should be quite easy to migrate a binary from one machine to the other (just need to have the same architecture and OS).