Cabal不构建可执行文件

时间:2015-04-21 16:45:00

标签: haskell cabal

出于某种原因,cabal没有为我的程序创建可执行文件。当我运行cabal build时,我得到了这个输出:

Building server-0.1.0.0...
Preprocessing executable 'server' for server-0.1.0.0...
Warning: output was redirected with -o, but no output will be generated
because there is no Main module.

后续的cabal run给了我这个错误:

Preprocessing executable 'server' for server-0.1.0.0...
Warning: output was redirected with -o, but no output will be generated
because there is no Main module.
cabal: dist/build/server/server: does not exist

果然,server中没有dist/build/server/server二进制文件。但是,临时文件位于dist/build/server/server-temp/中。

我的.cabal文件:

name:                server
version:             0.1.0.0
synopsis:            An example haskell web service.
license:             Apache-2.0
license-file:        LICENSE
author:              Some Body
maintainer:          somebody@gmail.com
category:            Web
build-type:          Simple
cabal-version:       >=1.10
executable server
  main-is:             Core/Main.hs
  build-depends:       base,
                       containers,
                       bytestring,
                       bytestring-conversion,
                       aeson,
                       http-types,
                       acid-state,
                       mtl,
                       safecopy,
                       warp,
                       wai,
                       wai-extra,
                       wai-middleware-static
  hs-source-dirs:      src
  default-language:    Haskell2010

我的目录结构(包括dist中的cabal build):

.
├── cabal.sandbox.config
├── dist
│   ├── build
│   │   ├── autogen
│   │   │   ├── cabal_macros.h
│   │   │   └── Paths_server.hs
│   │   └── server
│   │       └── server-tmp
│   │           ├── Core
│   │           │   ├── Main.dyn_hi
│   │           │   ├── Main.dyn_o
│   │           │   ├── Main.hi
│   │           │   └── Main.o
│   │           ├── Model
│   │           │   ├── DB.dyn_hi
│   │           │   ├── DB.dyn_o
│   │           │   ├── DB.hi
│   │           │   └── DB.o
│   │           └── Util
│   │               ├── HTTP.dyn_hi
│   │               ├── HTTP.dyn_o
│   │               ├── HTTP.hi
│   │               └── HTTP.o
│   ├── package.conf.inplace
│   │   └── package.cache
│   └── setup-config
├── LICENSE
├── server.cabal
├── Setup.hs
└── src
    ├── Core
    │   └── Main.hs
    ├── Model
    │   └── DB.hs
    ├── Service
    └── Util
        └── HTTP.hs

main定义:

main :: IO ()
main = do
  db <- openLocalStateFrom "~/.acid-state" (UserDatabase Map.empty)
  putStrLn $ "http://localhost:8080/"
  run 8080 $ logStdout $ staticPolicy (noDots >-> addBase "../client/") $ app db

1 个答案:

答案 0 :(得分:2)

消息中包含了所有内容:

您需要一个文件来定义 Main 模块

module Main where ...

并导出main函数:

main :: IO ()
main = ...

并且 .cabal 文件中的main-is字段应指向此内容。

如果您拥有所有这些,您应该能够将其编译成可执行文件。

这是一个很好的介绍:How to write a Haskell programm

更新

因为结果是模块名为Core.Main - 请确保您还有一个module Main - 您始终可以添加顶级main.hs并仅重新导出main来自Core.Main