Questions tagged [ffi]

A foreign function interface (FFI) is a mechanism for one language to interact with software written in another language.

1778 questions
24
votes
1 answer

How to call C++ code from Node.js?

I'm currently developing a simulator that runs on a server and should display data in the browser. For serving files, communication and things like that, I'd like to use Node.js. But, I'm not sure if it will perform as well as I'd want it to in the…
corazza
  • 31,222
  • 37
  • 115
  • 186
23
votes
1 answer

OSX, ghci, dylib, what is the correct way?

I need to build some C code and then reference that C code via the FFI. I would like to use my binding from inside ghci on osx. On of my constraints is that I cannot just hand the C sources to ghc in the .cabal file. This is due to a limitation…
Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
23
votes
14 answers

ERROR: Error installing ffi: ERROR: Failed to build gem native extension

I am getting this error in Mac 64 bit and have xcode inatalled on machine. Building native extensions. This could take a while... ERROR: Error installing ffi: ERROR: Failed to build gem native…
reddyonrails
  • 225
  • 1
  • 2
  • 5
23
votes
3 answers

Interchange structured data between Haskell and C

First, I'm a Haskell beginner. I'm planning integrating Haskell into C for realtime game. Haskell does logic, C does rendering. To do this, I have to pass huge complexly structured data (game state) from/to each other for each tick (at least 30…
eonil
  • 83,476
  • 81
  • 317
  • 516
23
votes
5 answers

Generate C wrapper from C++?

I want to generate C wrappers from C++ libraries. There are tutorials on how to do it by hand: http://dsc.sun.com/solaris/articles/mixing.html http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html But it is too much of a manual labor. For…
numeric
  • 475
  • 3
  • 15
22
votes
4 answers

Can the FFI deal with arrays? If so, how?

I'm pretty sure it's possible to send arrays through the FFI, but I can't find any examples. For instance, I have a Haskell array that I send to a int foo(int*) function, or I have a C array int bar[64]; that I send to Haskell. Ideally I'd want the…
Pubby
  • 51,882
  • 13
  • 139
  • 180
22
votes
2 answers

Compiling ghc with -fPIC support

I'm trying to install GHC with -fPIC support in Fedora. I've grabbed a source tarball since it seems no binary one has this. In Build.mk i've changed the quick build type to ifeq "$(BuildFlavour)" "quick" SRC_HC_OPTS = -H64m -O0 -fasm…
Phyx
  • 2,697
  • 1
  • 20
  • 35
22
votes
3 answers

Best way to call Haskell functions from within Java

I'm looking an efficient way of executing Haskell functions from within a Java program. I've considered the use of exec() to interact with GHC, but it seems like there should be a better method.
Viral Shah
  • 2,888
  • 2
  • 22
  • 25
21
votes
2 answers

How to properly link object files written in Haskell?

Roughly following this tutorial, I managed to get this toy project working. It calls a Haskell function from a C++ program. Foo.hs {-# LANGUAGE ForeignFunctionInterface #-} module Foo where foreign export ccall foo :: Int -> Int -> IO Int foo…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
21
votes
1 answer

What's the difference between extern fn and extern "C" fn in Rust?

I've tried reading various github issues trying to track down what the difference is and just ended up confused. #[no_mangle] pub extern fn foo() { ... } vs. #[no_mangle] pub extern "C" fn foo() { ... }
marathon
  • 7,881
  • 17
  • 74
  • 137
21
votes
1 answer

Rust FFI. Casting to void pointer

I've a function which has prototype as below //opaque struct struct mosquitto; struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *obj); In my c code, I'm calling it as below. struct mosquitto *m = mosquitto_new(buf, true,…
tez
  • 4,990
  • 12
  • 47
  • 67
21
votes
1 answer

Is it possible to use Fiddle to pass or return a struct to native code?

I would like to use Fiddle to access a native library compiled from Rust code. The C representation of the struct is very simple, it is just a pointer and a length: typedef struct { char *data; size_t len; } my_thing_t; // Example function…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
20
votes
1 answer

How exactly does PhantomData work in Rust?

I find the concept of PhantomData in Rust quite confusing. I use it extensively to constrain object lifetimes in my FFI-based code, and I am still not sure whether I do it correctly or not. Here's a contrived sample of how I often end up using it.…
Roman Dmitrienko
  • 3,375
  • 3
  • 37
  • 48
20
votes
4 answers

In C, given a variable list of arguments, how to build a function call using them?

Suppose there's a list of arguments stored somehow, in a array for example. Given a function pointer, how could I make a call to it passing the stored list of arguments? I'm not trying to pass the array as an argument ok. You got it, ok? I want to…
oblitum
  • 11,380
  • 6
  • 54
  • 120
19
votes
2 answers

How can I specify linker flags/arguments in a build script?

I'm using Rust, bindgen, and a build script to work on some FFI bindings to a library. This library is built using OpenMP, so when linking against it, I'd normally pass the -fopenmp flag to the compiler. How can I get this flag to be set by build.rs…
Dave Challis
  • 3,525
  • 2
  • 37
  • 65