1
これは初心者の質問です。私はHaskellからC関数を呼び出そうとしています。私は現在Windows上でスタックを使用しています。私はうまく機能次の簡単な例を開始しました:ハスケルWindows上でffiを呼び出す方法
import Prelude hiding (sin)
import Foreign.C -- get the C types
-- pure function
foreign import ccall "sin" c_sin :: CDouble -> CDouble
sin :: Double -> Double
sin d = realToFrac (c_sin (realToFrac d))
main = do
print . sin =<< readLn
その後、私は別のカスタムmyfunc
機能を追加しようとしました:
{-# INCLUDE "myfunction.h" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
import Prelude hiding (sin)
import Foreign.C -- get the C types
-- pure function
foreign import ccall "sin" c_sin :: CDouble -> CDouble
foreign import ccall "myfunc" c_myfunction :: Double -> Double
sin :: Double -> Double
sin d = realToFrac (c_sin (realToFrac d))
myfunc :: Double -> Double
myfunc d = realToFrac(c_myfunction d)
main = do
print . sin =<< readLn
print . myfunc =<< readLn
私は次のエラー取得しています:私は
src\Main.hs:1:12: warning:
-#include and INCLUDE pragmas are deprecated: They no longer have any effect
Linking src\Main.exe ...
src\Main.o:fake:(.text+0x56): undefined reference to `myfunc'
collect2.exe: error: ld returned 1 exit status
`gcc.exe' failed in phase `Linker'. (Exit code: 1)
を対応するヘッダmyfunction.h
でに通常のc関数としてmyfunc
を定義しています。
アプリをコンパイルするために私は私の情報源のstack ghc src/Main.hs src/myfunction.c
すべては私のプロジェクトのsrc
ディレクトリの下に配置されている使用しています。
{-# INCLUDE "myfunction.h" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
import Prelude hiding (sin)
import Foreign.C -- get the C types
-- pure function
foreign import ccall "sin" c_sin :: CDouble -> CDouble
foreign import ccall "myfunction" c_myfunction :: Double -> Double
sin :: Double -> Double
sin d = realToFrac (c_sin (realToFrac d))
myfunction :: Double -> Double
myfunction d = realToFrac(c_myfunction d)
main = do
print . sin =<< readLn
print . myfunction =<< readLn
はまた、あなたが外国importディレクティブでc_
を使用してC関数を参照してくださいする必要があります。