使用阴谋时插件包装未知符号

时间:2013-12-06 14:38:38

标签: haskell plugins ghc cabal cabal-install

我正在搞乱插件包但是我碰到了一个问题。

以下是代码:

的Util / Header.hs

module Util.Header(PT(..)) where

data PT a = PT a deriving Show

Plug.hs

module Plug(helloPlugin) where
import Util.Header
helloPlugin :: PT Int
helloPlugin = PT 1

Main.hs

module Main where

import Util.Header
import System.Plugins

main :: IO ()
main = do
    mv <- load "Plug.o" ["."] [] "helloPlugin"
    case mv of
        LoadFailure msg -> print msg
        LoadSuccess _ v -> print $ show (v :: PT Int)

这一切都正常,然后用ghc编译。使用Cabal构建工作也很好,但是当我运行可执行文件时出现此错误:

plugintest: /home/kevin/.cabal/lib/plugins-1.5.4.0/ghc-7.6.3/HSplugins-1.5.4.0.o: unknown symbol `ghczm7zi6zi3_ErrUtils_zdsinsertzuzdsgo5_info'
plugintest: user error (resolvedObjs failed.)

我极简主义的阴谋文件:

name:                plugintest
version:             0.1.0.0
license-file:        LICENSE
build-type:          Simple
cabal-version:       >=1.8

library
  hs-source-dirs: src
  exposed-modules: Util.Header
  build-depends:  base ==4.6.*, plugins ==1.5.*

executable plugintest
  main-is: Main.hs
  build-depends:  base ==4.6.*, plugins ==1.5.*, plugintest == 0.1.0.0
  hs-source-dirs: src

现在我假设问题是它无法找到“ErrUtils”模块,它是/usr/lib/ghc-7.x.x中安装的ghc软件包的一部分。 由于它使用的是cabal,因此它将使用$ HOME / .cabal / lib /。

现在我显然不想使用/ usr / lib,如果我想让它可以分发。遗憾的是,我不太熟悉如何管理包,也不熟悉插件包。

我有一种感觉,这是非常极端但我自己无法找到解决方案。

所以有几个问题:

  1. 如何让我的依赖项以某种方式工作以使其可分发?
  2. 看来我需要事先知道我的Plugin.o文件在实际使用之前会依赖什么(如果我理解正确的话)。 有没有办法打包.o文件,我不必担心这个问题? (对不起,如果这个问题太模糊,可以随意忽略)
  3. 提前致谢!

2 个答案:

答案 0 :(得分:4)

好的,所以我遇到了完全相同的问题。 这是我找到的解决方法

将加载调用更改为

load "Plug.o" [".","dist/build/plugintest/plugintest-tmp"] [] "testplugin"

确保使用-c或使用插件中的“make”库编译事物。

对此非常恼火...错误表明它存在与标准库链接的问题,那么为什么要显示这些 .o文件修复它? 无论如何,这对我有用,并且不需要大量的.cabal文件。

答案 1 :(得分:3)

您必须声明exported-other-模块,以便Cabal将它们打包在一起。例如(来自https://github.com/tel/happstack-heroku-test

name:                hktest         -- note the name here names
                                    -- the *library* which is a package name
                                    -- in scope when building the executable

...

library
  exposed-modules:     
    HKTest
  other-modules:
    -- there aren't any, but there could be some
  build-depends:       base                >= 4.6    && <4.7

                       ...

                     , mtl                 >= 2.1.2
  hs-source-dirs:      src

executable server
  main-is:             Server.hs
  other-modules:
    -- there might be some use to having these here,
    -- but they'll be harder to get into GHCi, so I wouldn't
    -- recommend it---just put them in the library part
  build-depends:       base >=4.6 && <4.7
                     , hktest                  -- note that I grab all the hktest
                                               -- modules here  
  hs-source-dirs:      exe

如果我遗漏其中一个模块,我可能会收到构建错误,因为Cabal会编译希望能够找到尚未打包的符号的文件。

在您的情况下,由于您正在构建可执行文件,因此上面列举的常见模式是将所有代码放入库中,然后让可执行文件依赖于该库。例如,在此示例中,exe/Server.hs的完整文本是

module Main where

import qualified HKTest as HK

main :: IO ()
main = HK.main