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?
5
votes
I clicked on the link and may I just say that the new GHC docs look marvellous!
- epsilonhalbe
I think the difference lies with the import of plain c-files and header files, but this is only guessing, I have actually never used the FFI.
- epsilonhalbe
1 Answers
6
votes
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.