ghc 编译的二进制文件是否需要 GHC 还是它们是独立的?

发布于 2024-11-15 18:00:46 字数 103 浏览 2 评论 0原文

如果朋友想要运行我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有心的人 2024-11-22 18:00:46

GHC 确实会生成不需要安装 GHC 本身的独立二进制文件,但是它们确实链接到一些动态库,尤其是 libgmp。其余的库通常在大多数 Linux 系统上都是开箱即用的。我相信 Windows 上的情况也类似。

您可以在 Linux 上使用 ldd 检查您所依赖的动态库。以下是我在 Ubuntu Natty 上得到的一个简单的 Hello World 程序:

$ echo 'main = putStrLn "Hello World"' > Hello.hs                                                   
$ ghc --make Hello.hs                                                                     
[1 of 1] Compiling Main             ( Hello.hs, Hello.o )
Linking Hello ...
$ ldd Hello                                                                                
    linux-vdso.so.1 =>  (0x00007fffe45ff000)
    libgmp.so.3 => /usr/lib/libgmp.so.3 (0x00007f8874cf9000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8874a74000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f887486b000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8874667000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f88742d3000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f88740b4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f8874f7a000)

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:

$ echo 'main = putStrLn "Hello World"' > Hello.hs                                                   
$ ghc --make Hello.hs                                                                     
[1 of 1] Compiling Main             ( Hello.hs, Hello.o )
Linking Hello ...
$ ldd Hello                                                                                
    linux-vdso.so.1 =>  (0x00007fffe45ff000)
    libgmp.so.3 => /usr/lib/libgmp.so.3 (0x00007f8874cf9000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8874a74000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f887486b000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8874667000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f88742d3000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f88740b4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f8874f7a000)
自找没趣 2024-11-22 18:00:46

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.

梦中楼上月下 2024-11-22 18:00:46

如果需要将某些 C 库与 Haskell 可执行文件静态链接,在 Linux 上,您可以将 --whole-archive 与 GNU 链接器一起使用;例如:

  ghc --make HelloZ.hs \
    -optl-Wl,--whole-archive \
      -optl/usr/lib/x86_64-linux-gnu/libffi.a \
      -optl/usr/lib/x86_64-linux-gnu/libz.a \
    -optl-Wl,--no-whole-archive

虽然它们很常见,但 libffilibz 并不是普遍存在的(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:

  ghc --make HelloZ.hs \
    -optl-Wl,--whole-archive \
      -optl/usr/lib/x86_64-linux-gnu/libffi.a \
      -optl/usr/lib/x86_64-linux-gnu/libz.a \
    -optl-Wl,--no-whole-archive

While they're quite common, libffi and libz 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.

蘑菇王子 2024-11-22 18:00:46

大多数二进制文件不需要安装 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文