gpt4 book ai didi

haskell - 交叉编译haskell代码时如何安装依赖项?

转载 作者:行者123 更新时间:2023-12-03 20:50:23 26 4
gpt4 key购买 nike

我已经成功创建了一个 ghc 交叉编译器,它允许我从我的 x64 linux 机器上为 armv6h(在我的例子中是树莓派)编译 haskell 代码。
我已经在树莓派上成功运行了一个 hello world 程序。

不,我想构建我真正的应用程序,它对其他 haskell 模块有很多依赖。
当我为 x64 编译时,我只是做

cabal install dependenciy1 depenency2 ...

我知道我可以让我自己的程序成为一个 cabal-project 自动化这一步。但这不是重点。

当我尝试使用交叉编译器时
arm-unknown-linux-gnueabi-ghc --make myapp.hs

它告诉我它找不到的模块。当然,它们没有安装!

我读了 https://ghc.haskell.org/trac/ghc/wiki/Building/CrossCompiling
并据此我尝试过
cabal --with-ghc=arm-unknown-linux-gnueabi-ghc --with-ghc-pkg=arm-unknown-linux-gnueabi-ghc-pkg --with-ld=arm-unknown-linux-gnueabi-ld install random

random 是我要在这里安装的依赖项。我收到以下错误:
Resolving dependencies...
Configuring random-1.0.1.3...
Failed to install random-1.0.1.3
Last 10 lines of the build log ( /home/daniel/.cabal/logs/random-1.0.1.3.log ):
/home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804: /home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804: cannot execute binary file
cabal: Error: some packages failed to install:
random-1.0.1.3 failed during the configure step. The exception was:
ExitFailure 126

当我做
file /home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804

我明白了
/home/daniel/.cabal/setup-exe-cache/setup-Cabal-1.18.1.3-arm-linux-ghc-7.8.3.20140804: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.10.2, not stripped

难怪它不能执行。它是为 arm 编译的。

我在这里错过了什么吗?
我的目标是引入所有依赖项,然后创建一个可以部署在我的树莓派上的静态链接应用程序。

最佳答案

要理解这个错误,你需要知道 cabal install在内部工作。本质上,它将执行以下步骤:

  • 下载并解压源代码
  • 编译Setup.hs (此文件用于自定义构建系统,例如,您可以在 configure 阶段实现一些 Hook 来运行额外的 haskell 代码)。
  • 运行setup configure <configure flags> && setup build && setup install

  • 现在的问题是 cabal install使用 --with-ghc 给出的 GHC也适用于第 2 步,但该步骤生成的可执行文件必须在主机系统上运行!

    一种解决方法是手动执行这些步骤,这意味着您可以完全控制。首先,获取源码:
    $ cabal get random
    Downloading random-1.0.1.3...
    Unpacking to random-1.0.1.3/
    $ cd random-1.0.1.3

    然后,编译 Setup.hs ,使用主机 ghc:
    $ ghc ./Setup.hs -o setup

    最后,配置、构建和安装。正如@Yuras 在评论中所建议的那样,我们还添加了 -x运行选项 hsc2hs :
    $ ./setup configure ----with-ghc=arm-unknown-linux-gnueabi-ghc --with-ghc-pkg=arm-unknown-linux-gnueabi-ghc-pkg --with-ld=arm-unknown-linux-gnueabi-ld --hsc2hs-options=-x
    $ ./setup build && ./setup install

    已经有一个关于这个的 cabal 问题: https://github.com/haskell/cabal/issues/2085

    关于haskell - 交叉编译haskell代码时如何安装依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25765893/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com