A foreign function interface (FFI) is a mechanism for one language to interact with software written in another language.
Questions tagged [ffi]
1778 questions
16
votes
1 answer
How to dereference a memory location from python ctypes?
I want to replicate the following c code in python ctypes:
main() {
long *ptr = (long *)0x7fff96000000;
printf("%lx",*ptr);
}
I can figure out how to call this memory location as a function pointer but not just do a normal dereference:
from…
user188565
15
votes
3 answers
How do I initialize an opaque C struct when using Rust FFI?
Here's what I would like to do in C code:
#include
int main() {
some_lib_struct_t x;
some_lib_func(&x);
}
How do I make use of the library in Rust? Here's what I've got so far:
extern crate libc; // 0.2.51
struct…

EFTH
- 475
- 5
- 14
15
votes
1 answer
Idiomatic Usage of Haskell Foreign Types?
Problem: Going from Haskell types to Foreign types and back requires a lot of boilerplate code.
For example, suppose we're working with the following Haskell data structure:
data HS_DataStructure = HS_DataStructure {
a1 :: String
, …

George
- 6,927
- 4
- 34
- 67
15
votes
1 answer
Why does Rust export its whole standard library when building a DLL?
I'm currently trying to write a dynamic library with Rust which will be loaded from a existing program. I need to export a few functions with specific names and calling conventions. Everything works, but as soon as I use anything from the standard…

dbeinder
- 153
- 1
- 6
15
votes
2 answers
Catching panic! when Rust called from C FFI, without spawning threads
I'm working on a Rust wrapper for the Duktape JavaScript interpreter. In a normal use case, the call stack will look like this:
Rust: Arbitrary application code.
Rust: My library wrapper.
C: The Duktape interpreter.
Rust: My Rust code.
Rust:…

emk
- 60,150
- 6
- 45
- 50
15
votes
2 answers
How do I use C preprocessor macros with Rust's FFI?
I'm writing some code that interfaces an existing library written in C. In my Rust code I'd like to be able to use values from CPP macros. If I have a C include.h that looks like this:
#define INIT_FLAG 0x00000001
I'd like to be able to use it in…

jdeseno
- 7,753
- 1
- 36
- 34
15
votes
1 answer
cabal FFI dependency
I am making a small Haskell game in Windows, where I would like to respond each time the user presses a key. Because getChar behaves strangely on Windows, I use FFI to get access to getch in conio.h, as described here. The relevant code is:
foreign…

Boris
- 5,094
- 4
- 45
- 71
14
votes
3 answers
I can't install ffi gem - MacOS Big Sur 11.0.1
I'm trying this Jekyll theme
And I referenced this docs
And This error code occurs endless!!
Why this error occurs?
Ah, For your information, I proceeded this & this
Even if I search error message, the data doesn't come out well!
I've been doing…

learner
- 149
- 1
- 2
- 7
14
votes
1 answer
Dart/Flutter ffi (Foreign Function Interface) native callbacks eg: sqlite3_exec
Hello I am using dart:ffi to build an interface with my native c/c++ library.
and I needed a way to get a callback from c to dart as an example in sqlite:
int sqlite3_exec(
sqlite3*, /* An open database */
…

Ala'a Al Hallaq
- 455
- 5
- 11
14
votes
3 answers
How to call a C++ dynamic library from Rust?
I want to call a C++ dynamic library (*.so) from Rust, but I don't want to build it from Rust. Like this,
cc::Build::new()
.file("src/foo.cc")
.shared_flag(true)
.compile("libfoo.so");
In some cases, I only need to call several…

Chi Wei Shen
- 307
- 1
- 2
- 8
14
votes
2 answers
What does % mean in an OCaml external declaration?
Many external declarations in the OCaml standard library have a % at the beginning of the function name, such as the definition of int_of_float:
external int_of_float : float -> int = "%intoffloat"
What does the '%' mean?

Michael Ekstrand
- 28,379
- 9
- 61
- 93
14
votes
4 answers
Call a Haskell function in .NET
I want to use a Haskell function with the following type ::
string -> string from a C# program.
I want to use hs-dotnet to bridge both worlds. The author claim that it's possible, but provide no sample of this case. The only samples provided are the…

Raoul Supercopter
- 5,076
- 1
- 34
- 37
14
votes
2 answers
Passing a set of NumPy arrays into C function for input and output
Let's assume we have a C function that takes a set of one or more input arrays, processes them, and writes its output into a set of output arrays. The signature looks as follows (with count representing the number of array elements to be…

apl
- 143
- 1
- 6
13
votes
5 answers
Is there a simple way to use Python libraries from Common Lisp?
One thing I really miss when writing Common Lisp code is access to Python libraries, both standard library and third party modules. CLPython provides a limited subset of Python functionality which precludes the use of most libraries, so that's not…

postfuturist
- 22,211
- 11
- 65
- 85
13
votes
5 answers
Error installing gems that use native extensions on Ubuntu, Ruby 1.9.2 via RVM
I get an error while trying to install the ffi gem:
~ - 16:54>gem i ffi
Building native extensions. This could take a while...
ERROR: Error installing ffi:
ERROR: Failed to build gem native extension.
rake…

Michiel de Mare
- 41,982
- 29
- 103
- 134