[英]cabal FFI dependency
我正在Windows中制作一個小型Haskell游戲,我希望每次用戶按下一個鍵時都會響應。 因為getChar
奇怪的行為在Windows上,我用FFI以訪問getch
在conio.h
,描述在這里 。 相關代碼是:
foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt
當我在ghci中運行它或使用ghc編譯它時,這很好。 我也想嘗試制作一個cabal包,所以從這個問題延伸,我在我的cabal文件中包含以下內容:
...
executable noughts
Includes: conio.h
Extra-libraries conio
...
但是當我運行cabal configure
,它告訴我:
cabal: Missing dependency on a foreign library:
* Missing C library: conio
這是有道理的,因為在我的haskell平台目錄下,在...\\Haskell Platform\\2012.4.0.0\\mingw
下, include
目錄下有一個conio.h
文件,但沒有其他conio
文件提供目標代碼。
我是以正確的方式做到這一點,如果是這樣,我怎樣才能找出要包含在我的cabal文件中的庫?
首先,C頭文件和庫之間並不總是一對一的映射。 在這種情況下,在聲明的函數conio.h
可以在各種運行時庫,如找到crtdll
(不建議使用)或msvcrt
(優選的,我想)。
使用Windows上的Haskell平台,Cabal將在.\\mingw\\lib
(在您的Haskell平台目錄下)中查找這些庫:如果您要求msvcrt
,它將查找.\\mingw\\lib\\libmsvcrt.a
。 這個特定的庫應該已經與您的Haskell平台一起提供。 (如果要使用lib*.a
文件指向其他目錄,可以使用Cabal的--extra-lib-dirs
選項。)
一個很好的例子如下: 這是Main.hs
:
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign.C.Types
foreign import ccall unsafe "conio.h _putch" c_putch :: CInt -> IO ()
main :: IO ()
main = do
c_putch . toEnum . fromEnum $ '!'
c_putch . toEnum . fromEnum $ '\n'
這將是something-awesome.cabal
。
name: something-awesome
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.8
executable yay
main-is: Main.hs
build-depends: base ==4.5.*
includes: conio.h
extra-libraries: msvcrt
這應該工作正常:
c:\tmp\something-awesome> dir /B
Main.hs
something-awesome.cabal
c:\tmp\something-awesome> cabal configure
Resolving dependencies...
Configuring something-awesome-0.1.0.0...
c:\tmp\something-awesome> cabal build
Building something-awesome-0.1.0.0...
Preprocessing executable 'yay' for something-awesome-0.1.0.0...
[1 of 1] Compiling Main ( Main.hs, dist\build\yay\yay-tmp\Main.o )
Linking dist\build\yay\yay.exe ...
c:\tmp\something-awesome> dist\build\yay\yay.exe
!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.