11

I have a package with the following structure (okay, this is greatly simplified, but...)

app/
  src/
    Main.hs
  data/
    data.txt
  app.cabal
  Paths_app.hs
  Setup.hs

In Paths_app.hs I have:

module Paths_app where
getDataFileName :: FilePath -> IO FilePath
getDataFileName = return

and in Main.hs I have:

module Main where
import Paths_app
main = do
    file <- getDataFileName "data/data.txt"
    data <- readFile file
    putStrLn $ "Your data is: " ++ data

the relevant parts of my app.cabal file look like this:

name: app
version: 1.0
build-type: Simple
data-files: data/data.txt

executable foo
  build-depends: base, haskell98
  main-is: Main.hs
  hs-source-dirs: src

This builds fine (using cabal configure followed by cabal install) but the executable complains that it can't find the data.txt file. I've tried replacing the line

file <- getDataFileName "data/data.txt"

with

file <- getDataFileName "data.txt"

but the same thing occurs. Is there something obvious I'm doing wrong?

4

2 回答 2

1

问题是我在 Windows 系统上构建,当我使用返回的文件名将getDataFileName数据加载到我的程序中时,我没有转义反斜杠。

于 2012-05-24T15:16:26.297 回答
1

我试图重现它,但它对我来说很好。

在您描述的设置中,我不得不放弃对haskell98两者的依赖,basehaskell98提供Prelude. 此外,该文件Main无法编译,因为它使用关键字data作为变量名,所以我将变量重命名为dat. 但后来它工作得很好。

关于我的设置的一些信息:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.4.1

$ cabal --version
cabal-install version 0.13.3
using version 1.14.0 of the Cabal library 

$ ls ~/.cabal/bin/
...  foo  ...   

$ ls ~/.cabal/share/app-1.0/data/
data.txt
于 2012-04-27T13:01:07.867 回答