【问题标题】:Make GHCi load and interpret a module with a "foreign export" declaration (for FFI with C)?让 GHCi 加载并解释带有“外国出口”声明的模块(对于带有 C 的 FFI)?
【发布时间】:2023-04-06 05:47:01
【问题描述】:

我有一个模块 (Safe.hs) 与

foreign export ccall respond_hs :: CWString -> IO CWString

对于带有 C 的 FFI。

我想在 GHCi 中加载 Safe.hs 并用它评估一些东西。

但是ghci 加载失败(我指定了两个源文件,因为它依赖于valencies.lhs):

$ ghci src/valencies.lhs src/Safe.hs 
GHCi, version 7.6.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 2] Compiling Valencies        ( src/valencies.lhs, interpreted ) [flags changed]

[2 of 2] Compiling Safe             ( src/Safe.hs, interpreted )

src/Safe.hs:10:1:
    Illegal foreign declaration: requires via-C, llvm (-fllvm) or native code generation (-fvia-C)
    When checking declaration:
      foreign export ccall "respond_hs" respond_hs
        :: CWString -> IO CWString
Failed, modules loaded: Valencies.
*Valencies> :q
Leaving GHCi.
$ 

提供-fvia-C 选项没有帮助。

相关问题

【问题讨论】:

    标签: haskell ghci ffi


    【解决方案1】:

    实际上,作为told by Dirk Thierbach,在这种情况下有两个有用的选项:

    如果您在 GHC 手册中查找 -fvia-C 部分标志参考, 您被重定向到第 4.10.6 节(影响代码的选项 一代)。你会在 -fvia-C 附近找到:

    -fobject-code

    生成目标代码。这是 GHCi 之外的默认设置,并且可以 与 GHCi 一起使用以优先生成目标代码 到字节码。

    -fbyte-code

    生成字节码而不是目标码。这是默认的 GHCI。字节码目前只能在交互中使用 解释器,未保存到磁盘。此选项仅适用于 反转-fobject-code的效果。

    这就解释了为什么它适用于 GHC,但不适用于 GHCI。

    所以,我现在很高兴ghci -fobject-code src/valencies.lhs src/Safe.hs

    相关问题

    • GHCi doesn't work with FFI export declarations/shared libaries 起初对我来说似乎是一团糟,因为它对我没有帮助。它也处理从 Haskell 到 C 的foreign export,但解决的问题是缺少一些目标文件,您必须将其提供给 GHCi 以使其链接所有内容。就我而言,我只是省略了与程序的 C 部分的链接,因为我不需要它来测试 Haskell 模块。重新阅读 Q&A 后,我怀​​疑给 GHCi 一个.o 可以简单地默默地将 GHCi 切换到正确的模式!

    如果您只想测试 Haskell 模块,并且它的函数可以独立于 C 函数工作(即它们不调用 C 函数),我相信我发现的选项比在命令行中添加更多 .o 文件以使 GHCi 链接所有内容(您会看到,可能需要进一步要求与其他包中的某些 C 函数链接,这对您来说并不重要)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 2019-02-08
      • 2021-03-29
      • 1970-01-01
      相关资源
      最近更新 更多