本文介绍了如何使用hsc2hs绑定常量,函数和数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $

  // foo 

.h
#define PI 3.14159

typedef struct {
int i1;
int i2;
} foo;

struct foo2 {
int i1;
int i2;
};

int fooFkt(foo f);

然后创建一个hsc文件:

  import Foreign 
import Foreign.C

#includefoo.h

使用 hsc2hs

  { - #INCLUDEfoo.h# - } 
{ - #LINE 1test.hsc# - }
import Foreign
{ - #LINE 2 test.hsc# - }
import Foreign.C


{ - #LINE 5test.hsc# - }

我不明白,我以为hsc2hs会为我导入所有需要的东西,比如 PI



谁能给我一个更好的例子? RWH ,你需要介绍一些东西它将C PI 值绑定到Haskell中的一个符号。



例如:

  { - #LANGUAGE ForeignFunctionInterface# - } 
{ - #LANGUAGE CPP# - }

导入外部
导入Foreign.C

- 助手
导入Control.Applicative
import Control.Monad

#includefoo.h

- 绑定符号

- 请注意,hsc2​​hs可以' t编组浮点CPP值,所以这将是3
- c_pi :: CInt
- c_pi = #const PI

- 相反,绑定到函数返回一个CFloat
pi':: Float
pi'= realToFrac c_pi

外部导入ccall不安全my_pic_pi :: CFloat

尽管我们在这里,我们也可以将结构编组到Haskell中,也可以从Haskell中调用结构:

   - 为struct foo编组

data Foo = Foo {i1,i2 :: Int}
派生Show

- 定义如何编组foo结构

实例可存储Foo其中
sizeOf _ =#{ size foo}

alignment _ = alignment(undefined :: CInt)

poke p foo = do
#{poke foo,i1} p $ i1 foo
#{poke foo,i2} p $ i2 foo

peek p = return Foo
`ap`(#{peek foo,i1} p)
`ap `(#{peek foo,i2} p)

绑定到 fooFkt 函数:

   - 导入函数

foreign import ccall foo.h fooFkt
c_fooFkt :: Ptr Foo - > IO CInt

- 从C

fooFkt :: Foo - > IO Int
fooFkt f = fromIntegral< $>与f c_fooFkt

完成。


i need a example how to use hsc2hs, i thought that when i write a header file like:

// foo.h
#define PI 3.14159

typedef struct {
    int i1;
    int i2;
} foo;

struct foo2 {
    int i1;
    int i2;
};

int fooFkt(foo f);

and then create a hsc file like:

import Foreign
import Foreign.C

#include "foo.h"

use hsc2hs:

{-# INCLUDE "foo.h" #-}
{-# LINE 1 "test.hsc" #-}
import Foreign
{-# LINE 2 "test.hsc" #-}
import Foreign.C


{-# LINE 5 "test.hsc" #-}

i dont understand it, i thought hat hsc2hs will import all needed things for me like the PI

who can give me a better example?

解决方案

Following RWH ch 17, you need to introduce something that binds the C PI value to a symbol in Haskell.

For example:

{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP                      #-}

import Foreign
import Foreign.C

-- helpers
import Control.Applicative
import Control.Monad

#include "foo.h"

-- binding a symbol

-- Note that hsc2hs can't marshal floating point CPP values, so this will be "3"
-- c_pi :: CInt
-- c_pi = #const PI

-- Instead, bind to a function that returns a CFloat
pi' :: Float
pi' = realToFrac c_pi

foreign import ccall unsafe "my_pi" c_pi :: CFloat

And while we're here, we can also marshal the struct to and from Haskell:

-- Marshalling for the struct foo

data Foo = Foo { i1, i2 :: Int }
    deriving Show

-- Define how to marshal foo structs

instance Storable Foo where
    sizeOf    _ = #{size foo}

    alignment _ = alignment (undefined :: CInt)

    poke p foo  = do
        #{poke foo, i1} p $ i1 foo
        #{poke foo, i2} p $ i2 foo

    peek p = return Foo
              `ap` (#{peek foo, i1} p)
              `ap` (#{peek foo, i2} p)

And bind to fooFkt function:

-- Import the function too

foreign import ccall "foo.h fooFkt"
    c_fooFkt :: Ptr Foo -> IO CInt

-- Marshal data to and from C

fooFkt :: Foo -> IO Int
fooFkt f = fromIntegral <$> with f c_fooFkt

Done.

这篇关于如何使用hsc2hs绑定常量,函数和数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 01:21