우선 Windows 10 64 비트 및 하스켈 플랫폼 8.0.1을 사용한다고 지정합니다.Windows에서 GHCI를 사용하는 Haskell 외부 함수 인터페이스
다음 코드를 사용하여 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 ...
으로 잘 컴파일 할 수 있으며 완전히 실행됩니다. (내가 그것을 실행 후 1을 입력하면)
> Examples.exe
'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>
나는 그런 conio.h
를 사용해야하지만, 결과는 비관적 같다 "-lmsvcrt
"로, "실종 라이브러리를"로드하려고합니다.
> 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. Windows에서 hSetBuffering을 사용하는 방법을 알고있는 사람이 있습니까 (8 년 전 ghc ticket #2189에 게시되었습니다.)
에있을 것입니다 : 지정할 필요없이이 잘'stdio.h에 getchar'를 사용하여 리눅스에서 작동 1. 도서관, 그리고 2. 귀하의 접근 방식은 대략 정확합니다. – crockeea
@Eric 리눅스에서는 hSetBuffering 함수가 잘 작동하고 그 함수를 사용하여 _Bufferless Input_을 만들 수 있기 때문에이 경우에는 FFI가 필요 없습니다. 그러나이 방법은 Windows에서는 작동하지 않았습니다. –
필자는'getChar'와 연결시키려는 주된 질문만을 언급했습니다. 나는 완충 문제에 관해 당신을 도울 수 없다. – crockeea