首先,我指定我使用 Windows 10 64bit 和 Haskell Platform 8.0.1。
我尝试使用以下代码在 Windows 中使用 Haskell 的 FFI。
import Control.Monad
import Data.Char
import Foreign.C
getCh :: IO Char
getCh = liftM (chr . fromEnum) c_getch
foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt
main :: IO ()
main = getCh >>= \x -> print x
在此之后,我可以用 ghc 很好地编译它
> ghc Examples.hs
[1 of 1] Compiling Main ( Examples.hs, Examples.o )
Linking Examples.exe ...
它完全运行。
> Examples.exe
'1'
(当我在运行后输入 1 时)
但是,GHCI 会出现问题。当我将它加载到 ghci 时,我收到了这些消息。
> ghci Examples.hs
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Examples.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
ByteCodeLink: can't find label
During interactive linking, GHCi couldn't find the following symbol:
getch
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session. Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please send a bug report to:
glasgow-haskell-bugs@haskell.org
*Main>
我尝试加载“缺少的库”,例如-lmsvcrt
需要使用的“” conio.h
,但结果悲观相同。
> ghci -lmsvcrt Examples.hs
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Examples.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
ByteCodeLink: can't find label
During interactive linking, GHCi couldn't find the following symbol:
getch
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session. Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please send a bug report to:
glasgow-haskell-bugs@haskell.org
*Main>
GHCI 可能会加载库,因为当我尝试加载错误的库时,ghci 会打印出错误。
我尝试了其他几件事,例如使用ghci Examples.hs -fobject-code
, ghci -lmsvcrt Examples.hs -fobject-code
,甚至
ghci Examples.hs "-luser32" "-lgdi32" "-lwinmm" "-ladvapi32" "-lshell32"
"-lshfolder" "-lwsock32" "-luser32" "-lshell32" "-lmsvcrt" "-lmingw32"
"-lmingwex" "-luser32" "-lmingw32" "-lmingwex" "-lm" "-lwsock32" "-lgdi32" "-lwinmm"
在ghc Examples.hs -v5
.
可悲的是,没有什么对我main
有用,我找不到任何其他方法。
PS 有没有人知道如何在 Windows 中使用 hSetBuffering (它是在 8 年前在ghc ticket #2189中发布的。它不是固定的吗?)