A foreign function interface (FFI) is a mechanism for one language to interact with software written in another language.
Questions tagged [ffi]
1778 questions
19
votes
8 answers
bundle install is not successful cannot install ffi 1.9.9 osx 10.9
bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Enter your password to install the bundled RubyGems to your system:
Using addressable…

Mohamed Sobhy
- 427
- 1
- 3
- 7
18
votes
3 answers
How do I use Haskell's FFI on structs?
I have created the following C library for reading an image:
typedef struct {
unsigned int height;
unsigned int width;
unsigned char* red; //length=height*width
unsigned char* green;
unsigned char* blue;
} Contents;
Contents…

Daniel O
- 4,607
- 6
- 44
- 52
18
votes
1 answer
How does one interface with a C enum using Haskell and FFI?
Let's say charm.c has an enum key and a function get_key() that returns a key type value.
How can I expose a corresponding Haskell Key record and function getKey :: IO Key?
And how can I do this without manually specifying how every single enum…

mcandre
- 22,868
- 20
- 88
- 147
18
votes
5 answers
What is the equivalent Haskell type for C99 bool when using FFI?
I have a library which uses C99 bool data type and I would like to call it via FFI.
What is the corresponding type for C99 bool in Haskell? In Foreign.C.types there are CInt, CShort etc, but no CBool.
If there is no "correct" type for bool, what is…

Zouppen
- 1,214
- 11
- 17
17
votes
1 answer
Building a dynamic library with haskell and using it from C++
I want to build a dynamic library containing haskell functions. I work on linux and want to call this dynamic library from C++ code.
I used the example at http://wiki.python.org/moin/PythonVsHaskell and have the following files:
Test.hs:
{-#…

Heinzi
- 5,793
- 4
- 40
- 69
17
votes
1 answer
Why is there overhead calling Haskell functions from C?
I've noticed a significant overhead calling Haskell functions in C, much larger than the overhead of a native C function call. To distill the issue to its essence, I wrote a program that just initializes the Haskell runtime, runs a loop that calls…

Evan Jenkins
- 516
- 3
- 8
17
votes
2 answers
Why doesn't sleep work?
Why does c_sleep return immediately in the following code?
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign.C.Types
import Data.Time.Clock
import Control.Concurrent
foreign import ccall unsafe "unistd.h sleep"
c_sleep :: CUInt -> IO…

Zeta
- 103,620
- 13
- 194
- 236
17
votes
1 answer
Statically linking a C library with a Haskell library
I have a Haskell project that aims to create some C++ bindings. I've written the C wrappers and compiled them into a stand-alone statically linked library.
I'd like to write the Haskell bindings to link statically to the C wrappers so that I don't…

Deech
- 2,223
- 15
- 20
17
votes
2 answers
How to handle long running external function calls such as blocking I/O in Rust?
Editor's note: This question is from a version of Rust prior to 1.0 and uses terms and functions that do not exist in Rust 1.0 code. The concepts expressed are still relevant.
I need to read data provided by an external process via a POSIX file…

Zargony
- 9,615
- 3
- 44
- 44
16
votes
2 answers
What is the correct type for returning a C99 `bool` to Rust via the FFI?
A colleague and I have been scratching our heads over how to return a bool from (a.k.a. _Bool) back to Rust via the FFI.
We have our C99 code we want to use from Rust:
bool
myfunc(void) {
...
}
We let Rust know about myfunc using an…

Edd Barrett
- 3,425
- 2
- 29
- 48
16
votes
2 answers
Calling a Clojure Function from Haskell
Is it possible to call a Clojure function from Haskell (on the GHC), using the FFI or some other trick? Here I'm interested in staying within the bounds of GHC (i.e., not using Frege). I'm also interested in keeping the central program in Haskell…

George
- 6,927
- 4
- 34
- 67
16
votes
2 answers
Calling dynamically linked Haskell code from Rust
I'm trying to compile some Rust code with some Haskell code. I have a test system set up with a file, Fibonacci.hs with a function which computes fibonacci numbers in Haskell and exports the function as fibonacci_hs via Haskell's FFI (as here:…

chalkandpaste
- 173
- 6
16
votes
1 answer
Haskell Data instance for opaque data type
I'm writing an open source patch to use a font library, or rather the haskell bindings to a font library in C (FTGL).
I'm pointing to the Font type in one of the data structures, which is defined as follows:
type Font = Ptr Font_Opaque
data…

Elise Huard
- 241
- 1
- 7
16
votes
1 answer
Haskell C FFI: accessing static data structures
I have a question about the Haskell C FFI, specifically about accessing static data structures exported by a C library.
The C library I’m wrapping has static data structures like FOO_GEORGE below, exported in the following fashion:
static struct…

Richard E. Silverman
- 1,071
- 8
- 14
16
votes
2 answers
Calling Haskell library from C++
I'm building an application in Qt (C++) which uses library written in Haskell as a backend. How can I export interface of Haskell library to C++?
When functions in haskell library are using simple types like int or float it's not a problem but what…

remdezx
- 2,939
- 28
- 49