Skip to content

Foreign-functions interface


FFI (foreign function interface) is a mechanism by which a program written in one programming language can call routines or make use of services written in another.

Some programs may not know at compilation what arguments are to be passed to a function. For instance, an interpreter may be told at runtime about the number and types of arguments used to call a given function. Libffi can be used in such programs to provide a bridge from the interpreter program to compiled code.

This interface uses Libffi, pre-installed in many OSs. (Except for some Linux OS.)

KxSystems/ffi: pre-requisites
libffi/libffi/doc: documentation

Integration with kdb+

Ffi is an extension to kdb+ for loading and calling dynamic libraries using pure q. The main purpose of the library is to build stable interfaces on top of external libraries, or to interact with the operating system from q. No compiler toolchain or writing C/C++ code is required to use this library.

Understand what you are doing

You do not need to write C code, but you do need to understand what you are doing. You can easily crash the kdb+ process or corrupt data structures in memory with little information about what happened.

For example, when a q callback function passed to the foreign function fails, you might see an error message in the console but, as the foreign function cannot handle a q error, execution stops and crashes the entire application.

No support is offered for crashes caused by use of this library.


Alexander Belopolsky

We are grateful to Alexander Belopolsky for allowing us to adapt and expand on his original codebase.

Back to top