Als Erstes gebe ich an, dass ich Windows 10 64bit und Haskell Platform 8.0.1 verwende.Haskell Foreign Function Interface mit GHCI in Windows
Ich versuche, FFI von Haskell in Windows mit folgendem Code zu verwenden.
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
Danach kann ich diese kompilieren gut mit ghc
> ghc Examples.hs
[1 of 1] Compiling Main (Examples.hs, Examples.o)
Linking Examples.exe ...
und es läuft vollständig.
> Examples.exe
'1'
(Wenn ich 1 geben, nachdem es ausgeführt wird)
tritt jedoch das Problem mit GHCI. Wenn ich es nach Ghci lade, habe ich diese Nachrichten bekommen.
> 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>
Ich versuche, „fehlende Bibliothek“, wie „-lmsvcrt
“ zu laden, die conio.h
verwenden muss, aber Ergebnis ist pessimistisch gleich.
> 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 lädt wahrscheinlich die Bibliothek, da, wenn ich eine falsche Bibliothek zu laden, GHCI druckt Fehler darüber.
Ich versuche, einige andere Dinge, wie ghci Examples.hs -fobject-code
verwenden, ghci -lmsvcrt Examples.hs -fobject-code
und sogar
ghci Examples.hs "-luser32" "-lgdi32" "-lwinmm" "-ladvapi32" "-lshell32"
"-lshfolder" "-lwsock32" "-luser32" "-lshell32" "-lmsvcrt" "-lmingw32"
"-lmingwex" "-luser32" "-lmingw32" "-lmingwex" "-lm" "-lwsock32" "-lgdi32" "-lwinmm"
die in ghc Examples.hs -v5
gefunden wurde.
Leider funktioniert nichts für meine main
, und ich kann keinen anderen Weg dafür finden.
P.S. Gibt es jemanden, der weiß, wie man hSetBuffering in Windows verwendet (es wurde vor 8 Jahren in ghc ticket #2189 veröffentlicht. Ist es nicht behoben?)
Ich kann Ihnen zwei Dinge nicht hilfreich nur sagen: 1. Die in Linux funktioniert gut 'stdio.h getchar' verwenden, ohne angeben zu müssen eine Bibliothek, und 2. Ihr Ansatz sieht ungefähr richtig aus. – crockeea
@Eric In Linux ist FFI in diesem Fall nicht nötig, da die hSetBuffering-Funktion gut funktioniert und ich mit dieser Funktion _Bufferless Input_ machen kann. Dieser Ansatz funktionierte jedoch nicht für Windows. –
Ich bezog mich nur auf Ihre Hauptfrage des Versuchens, mit 'getChar zu verbinden. Ich kann Ihnen beim Pufferungsproblem nicht helfen. – crockeea