我正在用Windows做一个小的Haskell游戏,我想在每次用户按下一个键的时候做出响应。因为Windows上的getChar behaves strangely,所以我使用FFI来访问conio.h中的getch,如here所述。相关代码为:
foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt当我在ghci中运行它,或者用ghc编译它时,它工作得很好。我还想尝试用它做一个cabal包,所以从this问题扩展,我在我的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文件中包含哪个库呢?
发布于 2012-11-25 19:13:29
首先,在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
!https://stackoverflow.com/questions/13544642
复制相似问题