Node:C interface, Previous:Macros, Up:Functions



C interface

When a Q function is compiled, the compiler emits various data structures the describe the function object (such as the kind of parameters it expects). It also emits a C++ function for the actual body of the Q function. Normally, the compiler generates a name for the emitted C++ function, but if you write external name after the parameter list, the emitted C++ function will be called name.

Example:

:(foo :x) external Foo = 3 + x

This emits C++ code something like:

Root * Foo(Root *x)
{
   return /* Code to calculate 3 + x */;
}

// Data structures that define foo function object
// These include the symbol 'foo, and a pointer to Foo.

The compiler also emits a .h file that includes a declaration for Foo. You can include this .h is some other C++ source program file, and in that program you can call Foo as if it were written in C++.

The converse problem is when you have a function written in C++ that you want to use in Q. You can do this by writing a dummy function definition whose body is just extern name:

:(bar :x)=external Bar

You can then call bar is your Q code, just as if it were a Q function, assuming you provide it a C++ function something like:

Root *Bar(Root *x)
{
    // Code you write to implement bar.
}

In both forms of external the name can be a quoted string instead of an identifier. In that case the string is the assembler label for the generated/needed C++ function, as opposed to the C++ name of the function.