讓我們假設你有一個mylib
庫,並mylib-commandline
和mylib-server
可執行文件。
您使用hs-source-dirs
磁帶庫和每個可執行文件,使每個人都有自己的項目的根,避免重複編譯:
mylib/ # Project root
mylib.cabal
src/ # Root for the library
tests/
mylib-commandline/ # Root for the command line utility + helper modules
mylib-server/ # Root for the web service + helper modules
完整的目錄佈局:
mylib/ # Project root
mylib.cabal
src/ # Root for the library
Web/
Mylib.hs # Main library module
Mylib/
ModuleA # Mylib.ModuleA
ModuleB # Mylib.ModuleB
tests/
...
mylib-commandline/ # Root for the command line utility
Main.hs # "module Main where" stub with "main = Web.Mylib.Commandline.Main.main"
Web/
Mylib/
Commandline/
Main.hs # CLI entry point
Arguments.hs # Programm command line arguments parser
mylib-server/ # Root for the web service
Server.hs # "module Main where" stub with "main = Web.Mylib.Server.Main.main"
Web/
Mylib/
Server/
Main.hs # Server entry point
Arguments.hs # Server command line arguments parser
的短柱入口點文件mylib-commandline/Main.hs
看起來像這樣:
module Main where
import qualified Web.Mylib.Server.Main as MylibServer
main :: IO()
main = MylibServer.main
您需要它們,因爲executable
必須在名爲Main
的模塊上啓動。
你mylib.cabal
看起來是這樣的:
library
hs-source-dirs: src
exposed-modules:
Web.Mylib
Web.Mylib.ModuleA
Web.Mylib.ModuleB
build-depends:
base >= 4 && <= 5
, [other dependencies of the library]
executable mylib-commandline
hs-source-dirs: mylib-commandline
main-is: Main.hs
other-modules:
Web.Mylib.Commandline.Main
Web.Mylib.Commandline.Arguments
build-depends:
base >= 4 && <= 5
, mylib
, [other depencencies for the CLI]
executable mylib-server
hs-source-dirs: mylib-server
main-is: Server.hs
other-modules:
Web.Mylib.Server.Main
build-depends:
base >= 4 && <= 5
, mylib
, warp >= X.X
, [other dependencies for the server]
cabal build
將生成庫和兩個可執行文件,而不庫的雙編譯,因爲每一個在自己hs-source-dirs
和可執行文件依賴於庫。
您仍然可以從您的項目的根目錄運行帶有runghc
可執行文件,使用-i
開關告訴它要尋找模塊(使用:
作爲分隔符):
runhaskell -isrc:mylib-commandline mylib-commandline/Main.hs
runhaskell -isrc:mylib-server mylib-server/Server.hs
這種方式,你可以有一個乾淨的佈局,帶有輔助模塊的可執行文件,並且所有內容仍然適用於runhaskell
/runghc
和ghci
。爲避免重複鍵入此標誌,您可以添加類似於
:set -isrc:mylib-commandline:mylib-server
到您的.ghci
文件。
請注意,有時應將代碼拆分爲單獨的包,例如, mylib
,mylib-commandline
和mylib-server
。
來源
2012-09-06 18:12:13
nh2
完美答案,我在官方的Haskell頁面上尋找這樣簡單而直接的文章,但沒有運氣。可能我沒有花足夠的時間搜索,但這對我有很大的幫助。謝謝! – korCZis