首先,我指定使用Windows 10 64位和Haskell Platform 8.0.1。Haskell在Windows中使用GHCI的外部函數接口
我嘗試使用以下代碼在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:
[email protected]
*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:
[email protected]
*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
,我無法找到任何其他途徑。
P.S.有沒有人知道如何在Windows中使用hSetBuffering(它在8年前發佈在ghc ticket #2189。是不是固定?)
我只能告訴你兩兩件事沒有幫助:1.這個工程在Linux下就好使用'stdio.h中getchar',而無需指定一個圖書館,和2.你的方法看起來大致正確。 – crockeea
@Eric在Linux中,在這種情況下不需要FFI,因爲hSetBuffering函數可以正常工作,並且通過使用該函數,我可以創建_Bufferless Input_。但是,這種方法不適用於Windows。 –
我只提到你試圖用'getChar'鏈接的主要問題。我無法幫助您解決緩衝問題。 – crockeea