Haskell如何在Windows上调用ffi
抱歉,如果这是一个新手问题。我试图从Haskell调用一个C函数。我目前正在Windows上使用堆栈。我从以下简单的例子开始,这个例子效果很好:Haskell如何在Windows上调用ffi,haskell,haskell-stack,Haskell,Haskell Stack,抱歉,如果这是一个新手问题。我试图从Haskell调用一个C函数。我目前正在Windows上使用堆栈。我从以下简单的例子开始,这个例子效果很好: 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
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
我得到以下错误:
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.c
中将myfunc
定义为一个常规的c函数,并带有相应的inc头myfunction.h
对于编译应用程序,我使用的是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
{-#包括“myfunction.h”#-}
{-#语言外来功能接口#-}
导入前奏隐藏(sin)
import Foreign.C——获取C类型
--纯函数
国外进口ccall“sin”c_sin::CDouble->CDouble
国外进口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
印刷品。罪=
{-# 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