A foreign function interface (FFI) is a mechanism for one language to interact with software written in another language.
Questions tagged [ffi]
1778 questions
104
votes
2 answers
How do I convert a C string into a Rust string and back via FFI?
I'm trying to get a C string returned by a C library and convert it to a Rust string via FFI.
mylib.c
const char* hello(){
return "Hello World!";
}
main.rs
#![feature(link_args)]
extern crate libc;
use libc::c_char;
#[link_args = "-L . -I .…

Dirk
- 2,094
- 3
- 25
- 28
81
votes
12 answers
CocoaPods on M1 (Apple Silicon) fails with ffi wrong architecture
Running 'pod install' on a M1 MacBook failed for me due to an ffi issue, as described here.
I followed some of the workarounds (I guess I tried all of them in various order), but now I get a slightly different error:
LoadError -…

Nusatad
- 3,231
- 3
- 11
- 17
67
votes
2 answers
Passing Numpy arrays to a C function for input and output
Oh my word I'm a fool.
I was simply omitting the second and third arguments when calling the function.
Like a fool.
Because that's what I am.
Original silly question follows:
This seems like it must be a very common thing to do, but I can't find a…

Tom Future
- 1,900
- 2
- 15
- 15
63
votes
10 answers
Python: SWIG vs ctypes
In python, under what circumstances is SWIG a better choice than ctypes for calling entry points in shared libraries? Let's assume you don't already have the SWIG interface file(s). What are the performance metrics of the two?

Kevin Little
- 12,436
- 5
- 39
- 47
60
votes
5 answers
Calling Haskell from C++ code
I'm currently writing an app in C++ and found that some of its functionality would be better written in Haskell. I've seen instructions on calling Haskell from C code, but is it possible to do the same with C++?
EDIT: To clarify, what I'm looking…

Tomer Vromen
- 1,762
- 1
- 13
- 13
52
votes
1 answer
Understand foreign function interface (FFI) and language binding
Mixing different programming languages has long been something I don't quite understand. According to this Wikipedia article, a foreign function interface (or FFI) can be done in several ways:
Requiring that guest-language functions which are…

Tim
- 1
- 141
- 372
- 590
50
votes
1 answer
GHCi runtime linker issue when using FFI declarations
I have a problem regarding FFI in Haskell and the interactive mode of GHC again.
Consider FFISo.hs:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString.Char8 as B
import FFIFun.Foo
main :: IO ()
main = do
…

lewurm
- 1,103
- 7
- 19
46
votes
2 answers
Calling Haskell from C#
I just spent the last week or so figuring out how to execute C++ code from C# as part of my day job. It took us forever to figure it out, but the final solution is fairly simple.
Now I'm curious... How hard would it be to call Haskell from C#? (Note…

MathematicalOrchid
- 61,854
- 19
- 123
- 220
46
votes
3 answers
How can a Windows service application be written in Haskell?
I've been struggling to write a Windows service application in Haskell.
Background
A service application is executed by the Windows Service Control Manager. Upon launching it makes a blocking call to StartServiceCtrlDispatcher which is supplied with…

Michael Steele
- 15,512
- 2
- 23
- 24
42
votes
4 answers
Mixing Haskell and C++
If you had the possibility of having an application that would use both Haskell and C++.
What layers would you let Haskell-managed and what layers would you let C++-managed ?
Has any one ever done such an association, (surely) ?
(the Haskell site…

Stephane Rolland
- 38,876
- 35
- 121
- 169
36
votes
6 answers
Calling Haskell functions from Python
I want to use some Haskell libraries (e.g. Darcs, Pandoc) from Python, but it seems there’s no direct foreign function interface to Haskell in Python. Is there any way to do that?

minhee
- 5,688
- 5
- 43
- 81
36
votes
1 answer
Working with c_void in an FFI
I am struggling with passing a struct through an FFI that accepts void and reading it back on the other end.
The library in question is libtsm, a terminal state machine. It allows you to feed input and then find out in which state a terminal would…

Skade
- 1,406
- 1
- 11
- 14
32
votes
2 answers
How to expose a Rust `Vec` to FFI?
I'm trying to construct a pair of elements:
array: *mut T
array_len: usize
array is intended to own the data
However, Box::into_raw will return *mut [T]. I cannot find any info on converting raw pointers to slices. What is its layout in memory?…

vinipsmaker
- 2,215
- 2
- 19
- 33
32
votes
3 answers
How do I convert a Rust closure to a C-style callback?
I'm trying to write a Rusty wrapper for a piece of C API. There is one C construct I struggle with:
typedef bool (*listener_t) (int, int);
bool do_it(int x1, int y1, int x2, int y2, listener_t listener)
The function does its job for a range of…

Tomo
- 3,431
- 5
- 25
- 28
31
votes
3 answers
Rails - TypeError: Unable to resolve type 'size_t' (Big Sur - M1)
I have an issue with my new mac to install a new rails app and I really need to resolve it quickly.I fixed all all problems with gem and bundle update is ok but I can not run rails s. I think it is because of the M1 but I don't find any post who fix…

Emz
- 321
- 3
- 4