hsc2hs和c2hs有什么区别?
我知道hsc2hs是一个预处理器,但它到底是做什么的?
c2hs可以用C代码制作Haskell模块,但是我需要hsc2hs吗?
我需要一个例子如何使用hsc2hs,我想当我写一个头文件,如:
// foo.h
#define PI 3.14159
typedef struct {
int i1;
int i2;
} foo;
struct foo2 {
int i1;
int i2;
};
int fooFkt(foo f);
Run Code Online (Sandbox Code Playgroud)
然后创建一个像以下一样的hsc文件:
import Foreign
import Foreign.C
#include "foo.h"
Run Code Online (Sandbox Code Playgroud)
用途hsc2hs:
{-# INCLUDE "foo.h" #-}
{-# LINE 1 "test.hsc" #-}
import Foreign
{-# LINE 2 "test.hsc" #-}
import Foreign.C
{-# LINE 5 "test.hsc" #-}
Run Code Online (Sandbox Code Playgroud)
我不明白,我认为帽子hsc2hs会像我一样为我导入所有需要的东西 PI
谁能给我一个更好的例子?
我正在尝试编写一个模拟Windows上击键的Haskell程序.我试图调用keybd_event和SendInput,但都没有编译.不过,我可以用解释器来运行程序.当我在winable.h中包含对SendInput的绑定时尝试构建程序时,我收到错误:
cabal install
...
[1 of 2] Compiling WindowsKeys ( dist\build\WindowsKeys\WindowsKeys-tmp\WindowsKeys.hs, dist\build\WindowsKeys\WindowsKeys-tmp\WindowsKeys.o )
[2 of 2] Compiling Main ( src\Main.hs, dist\build\WindowsKeys\WindowsKeys-tmp\Main.o )
Linking dist\build\WindowsKeys\WindowsKeys.exe ...
dist\build\WindowsKeys\WindowsKeys-tmp\WindowsKeys.o:fake:(.text+0x35d): undefined reference to `SendInput'
collect2: ld returned 1 exit status
cabal: Error: some packages failed to install:
WindowsKeys-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1
Run Code Online (Sandbox Code Playgroud)
详细错误发生在http://pastebin.com/trg21N0x,但它似乎不再包含任何线索.我尝试使用时遇到类似的错误keybd_event.我写的hsc文件包含以下标题:
#include "windows.h"
#include "winuser.h"
#include "winable.h"
Run Code Online (Sandbox Code Playgroud)
这是C绑定:
foreign import ccall unsafe "winable.h SendInput"
c_SendInput :: UINT
-> Ptr Input
-> CInt …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个与C通信的Haskell程序(最终通过GHC-iOS为iOS).我想让它从C传递一个字符串到Haskell,让Haskell处理它,然后通过hsc2s将一些数据类型从Haskell返回到C Structs.我找不到一个简单明了的教程是不成功的.Haskell唯一需要的是String,没有别的.
我对第一部分没有任何问题,将一个字符串传递给Haskell.
testPrint :: CString -> IO ()
testPrint c = do
s <- peekCString c
putStrLn s
Run Code Online (Sandbox Code Playgroud)
出于测试目的和将来的参考,我只希望能够处理如下的内容.
C结构
struct testdata {
char *a;
char *b;
int c;
};
Run Code Online (Sandbox Code Playgroud)
Haskell数据类型
data TestData = TestData {
a :: String,
b :: String,
c :: Int
} deriving Show
-- not sure what the type would look like
testParse :: CString -> TestData
Run Code Online (Sandbox Code Playgroud)
我知道我需要将TestData类型化为Storable并实现peek,poke,sizeOf和alignment,但我需要先看一个简单的例子才能真正理解它.大多数教程都需要外部库,使其比需要的更复杂.
以下是我看过的资源:
Stackoverflow - 如何使用hsc2hs绑定到常量,函数和数据结构?
编辑:目前我被卡住的地方(在C中调用setFoo时会出现分段错误)
Haskell代码片段
instance …Run Code Online (Sandbox Code Playgroud) cabal可以使用hsc2hs来创建hs文件吗?怎么样?
我没有找到在手册的答案,谷歌上搜索,也没有在其他项目(有我的希望为gtk2hs,但事实证明,它不使用的阴谋)
我正在尝试针对某些 C++ 源构建带有 FFI 的 Haskell 可执行文件。
我有一个像这样的 C 头文件 (cstuff/foo.h):
#ifndef _FOO_H_
#define _FOO_H_
#include <somecppheader.c> // Some header outside of my control with C++ constructs
void foo();
#endif
Run Code Online (Sandbox Code Playgroud)
foo 的实现应该无关紧要。它会做一些事情并使用一些在 somecppheader.h 中声明的东西。
Cabal 文件中的可执行部分如下所示:
executable botprog
main-is: Main.hsc
other-extensions: ForeignFunctionInterface
build-depends: base >=4.7 && <4.8
build-tools: hsc2hs
default-language: Haskell2010
include-dirs: cstuff
c-sources: cstuff/foo.cpp
pkgconfig-depends: somelib -- The one that contains somecppheader.h
Run Code Online (Sandbox Code Playgroud)
Main.hsc 看起来像这样:
{-# LANGUAGE ForeignFunctionInterface #-}
#include <foo.h>
foreign import ccall "foo.h foo" :: IO ()
main …Run Code Online (Sandbox Code Playgroud) 在Haskell程序中,使用C头中定义的常量的最佳方法是什么?