Search code examples
haskellffi

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?


Solution

  • 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.