Difference Between Ccall and Capi Ffi Calling Conventions

Difference Between Ccall and Capi Ffi Calling Conventions

I discovered that there are two calling conventions using GHC's FFI: ccall and capi. The docs don't have much information about the two conventions. What is the difference between them, and when should I use each one? Is one faster than the other?

2

1 Answer

ccall is the normal way. It works by directly linking against a symbol defined in a (usually written in C) library.

capi is a ghc extension that works on the C source level. That's why it can access things that don't exist at the ABI level, such as macros. (I don't know how it's implemented, but I would guess it generates a small C function wrapper, which it then compiles with a C compiler behind the scenes.)

I would use ccall where possible. It's part of the language standard and looks less "magical" in general.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.