6.32.8 How the C interface works

For every Forth word declared with c-function, the C interface generates a wrapper function in C that takes the Forth data from the Forth stacks, and calls the target C function with these data as arguments. After the C function returns, the return value is pushed on the appropriate stack.

During this process, type conversion may happen between the Forth type and the C type of an argument or return value. E.g. on a 64-bit platform, if the Forth argument is (64-bit) n, and the C argument is (32-bit) int, the n will be cast into the int (chopping off the 32 most-significant bits), while for a return value the same relation will lead into a sign-extension of the 32-bit int into a 64-bit n.

The \c lines are literally included in the C code (but without the \c), and provide the necessary declarations so that the C compiler knows the C types and has enough information to perform the conversion.

These wrapper functions are eventually compiled and dynamically linked into Gforth, and then they can be called.

The libraries added with add-lib are used in the compile command line to specify dependent libraries with -llib, causing these libraries to be dynamically linked when the wrapper function is linked.