Questions tagged [c2hs]

c2hs is a generator tool for Haskell bindings to C libraries.

C->Haskell (c2hs) is an interfacing tool that assists in the development of Haskell FFI bindings to C libraries. It gathers interface information like data type definitions and function signatures from C header files and generates Haskell code with foreign imports.

There are some distinctions between c2hs and hsc2hs.

References

Related Tags

38 questions
3
votes
1 answer

linker error in C2HS generated C binding

I build and run the following minimal example (no external dependencies) with C->Haskell Compiler, version 0.25.2 Snowboundest, 31 Oct 2014 build platform is "x86_64-darwin" <1, True, True, 1> ; build command: c2hs Structs.chs ghci…
ocramz
  • 816
  • 6
  • 18
3
votes
1 answer

Building with c2hs and cabal

I have a problem where cabal will not do dependency resolution on .chs files, that is, if A.chs depends on B.chs (or really B.chi) then cabal will not figure it out and call c2hs on the files in the correct order. I know that gtk2hs uses a custom…
HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
3
votes
1 answer

c2hs: Binding constants

I'm trying to write a quick binding to some ioctl functions (in particular, getting and setting the window size) using c2hs. Here's the relevant part of what I have: {-# LANGUAGE ForeignFunctionInterface #-} #include #include…
Impredicative
  • 5,039
  • 1
  • 17
  • 43
3
votes
1 answer

Using c2hs to marshal a void*

My C function looks like this: void *c_shm_create(char*, int); My .chs file looks like this: {-# LANGUAGE ForeignFunctionInterface #-} module System.Shm.Internal.Bindings ( c_shmCreate ) where #include "hs_shm.h" import C2HS {#fun unsafe…
kvanbere
  • 3,289
  • 3
  • 27
  • 52
3
votes
1 answer

c2hs - anonymous enums?

The c2hs reference (aka 'Tutorial') mentions the name of the C enum as a non-optional parameter. Is there any way to get it to work with anonymous enums? Determining which enum to generate should be possible given just a single enum member, or even…
Cubic
  • 14,902
  • 5
  • 47
  • 92
2
votes
1 answer

Haskell FFi with c2hs: Better out-marshalling of structs

Suppose you have a C API provides that provides a C struct typedef struct A { int i; float f; } A; and a function that populates it: void getA(A* a); For example, this could be a getter for some information from the innards of the C…
mcmayer
  • 1,931
  • 12
  • 22
2
votes
1 answer

Using c2hs on Windows

I'm trying to use the libxml-enumerator package on Windows, which (ultimately) needs to use c2hs. When trying to build the relevant package, I get the seemingly infamous "does not exist" error from c2hs. This seems to be related to not having GCC…
Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77
2
votes
1 answer

Error when trying to use a ForeignPtr argument to a dynamic wrapper

I'm trying to wrap a C library using c2hs. I've got an opaque C struct that I've mapped in Haskell as follows: {#pointer *foo as Foo foreign newtype #} I've used a foreign pointer so I can automatically clean up with a finalizer. All of that seems…
Adrian
  • 98
  • 5
2
votes
2 answers

GHCi linker error with FFI-imported MPI constants (via c2hs)

I'm figuring out how haskell-mpi works by rewriting the binding. I'm trying to re-use the MPICH installation that was set up by installing PETSc (which is working fine). Question: make main gives me a correct module in GHCi, but when I request to…
ocramz
  • 816
  • 6
  • 18
2
votes
1 answer

how to declare a callback to Haskell land

I am interfacing to a C library, and there's a function that has a callback argument of type (int (*fun) (void *)). How would I handle this with c2hs? I don't see callbacks mentioned…
d8d0d65b3f7cf42
  • 2,597
  • 15
  • 28
1
vote
1 answer

Asking stack to pass extra command-line arguments to c2hs

I use c2hs for easing the process of writing Haskell bindings to C/C++ libraries. I also recently started using stack for managing my Haskell projects. To use c2hs, I need to have a line saying #include "myheader.h" inside my Haskell source code;…
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
1
vote
1 answer

Large c2hs-inferred size of C enum

When writing the Storable instance of a C enum that has 5 options (using c2hs), the {# sizeof #} macro returns 4 (i.e. 4 bytes). Isn't this extremely wasteful, when 3 bits would suffice? Does this depend on the size of a memory word?
ocramz
  • 816
  • 6
  • 18
1
vote
1 answer

C2HS marshalling double pointer

When there is a function like: some_type_t* some_type_create(const char* name, char** errptr); is there a way to get C2HS to generate a Haskell binding with the following signature? someTypeCreate :: String -> IO (SomeTypeFPtr, String) Here is…
Alexey Raga
  • 7,457
  • 1
  • 31
  • 40
1
vote
1 answer

Cabal build with c2hs not finding .chs module

I am trying out c2hs, and wanted to compile a small example of a shared library with Cabal to get started. I have the following relevant section of the cabal file test.cabal: executable libtest.so hs-source-dirs: src main-is: …
rhaps0dy
  • 1,236
  • 11
  • 13
1
vote
0 answers

c2hs bind both typedef and function

I am trying to properly create haskell bindings for function in C, that is split up in 2 files. file1.h: typedef const char* fmi2GetTypesPlatformTYPE(void); file2.h: __declspec(dllexport) fmi2GetTypesPlatformTYPE fmi2GetTypesPlatform; To match this…
Casper Thule Hansen
  • 1,510
  • 2
  • 19
  • 36