Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell如何在Windows上调用ffi_Haskell_Haskell Stack - Fatal编程技术网

Haskell如何在Windows上调用ffi

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

抱歉,如果这是一个新手问题。我试图从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 = 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