Dear GAP Forum,
Dima Pasechnik asked about calling GAP from C or C++ programs.
The first thing to say is that it is not really possible to integrate GAP
directly as a C library into another program. The GAP memory manager, in
particular, is not a "good neighbour" and does not coexist well with other
memory managers, and it is not easily possible to separate GAP from its user
interface and get a system that will run without wanting to access standard
input, output and error.
It is possible to run GAP via a pipe or pseudo-tty as a slave process, pass
commands to it on stdin and parse the output from stdout. Using streams on the
GAP side, one could avoid GAP syntax on this link and just pass the necessary
There are (or more accurately will very shortly be) two ways in which you
could link your C program as a slave of GAP. Firstly, GAP 4.2 will contain new
functionality for "bidirectional streams" allowing a slave process to be
started by GAP, which GAP can then interact with via the stream. The streams
functionality is being released for the first time GAP 4.2, and may need some
enhancement, but I would be very happy to work with you on that.
The second alternative is a bit more esoteric. The GAP compiler gac, converts
files of GAP code into C code, which can be dynamically loaded as an extension
to the kernel. It is possible to take the output of the compiler and replace
the C code in it by your own code, thereby encapsulating your code as a
dynamically loadable kernel extension. Your code however must then comply with
the rules that apply to GAP kernel code, in particular the rules of the memory
manager, which are not too onerous, but need some care. On the other hand,
your C code would be free to interact with the GAP session very closely, using
the various interfaces which exist for kernel programming. The edim package,
by Frank Luebeck works in this way.