出于某种原因,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
答案 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