给定以下目录结构:
root
??? scripts
? ??? script1.hs
??? source
??? librarymodule.hs
??? libraryconfig.txt
Run Code Online (Sandbox Code Playgroud)
其中“librarymodule.hs”将是一个导出多个函数的库,其输出受其目录中 libraryconfig.txt 文件内容的影响。
script1.hs 是需要使用librarymodule.hs 中声明的函数的文件。
我无法在互联网上找到上述结构的解决方案,希望有人能提供帮助。
GHC 有一个-i选项。在 下root/scripts/,这将添加root/source/到搜索路径中:
ghc -i../source script1.hs
Run Code Online (Sandbox Code Playgroud)
还可以考虑使用打包您的库,cabal以便您可以安装它并在任何地方使用它而无需担心路径。
这是一个带有data-files以下库的最小示例:
source/
??? mylibrary.cabal
??? LibraryModule.hs
??? libraryconfig.txt
Run Code Online (Sandbox Code Playgroud)
mylibrary.cabalname: mylibrary
version: 0.0.1
build-type: Simple
cabal-version: >= 1.10
data-files: libraryconfig.txt
library
exposed-modules: LibraryModule
other-modules: Paths_mylibrary
build-depends: base
default-language: Haskell2010
Run Code Online (Sandbox Code Playgroud)
LibraryModule.hsmodule LibraryModule where
import Paths_mylibrary -- This module will be generated by cabal
-- Some function that uses the data-file
printConfig :: IO ()
printConfig = do
n <- getDataFileName "libraryconfig.txt"
-- Paths_mylibrary.getDataFileName resolves paths for files associated with mylibrary
c <- readFile n
print c
Run Code Online (Sandbox Code Playgroud)
有关该Paths_*模块的信息,请参阅此链接:https : //www.haskell.org/cabal/users-guide/developing-packages.html#accessing-data-files-from-package-code
现在运行cabal install应该安装mylibrary.
然后,在 下scripts/script1.hs,您可以ghc script1.hs使用您安装的库运行。
| 归档时间: |
|
| 查看次数: |
1341 次 |
| 最近记录: |