Next: Example
Up: Calling C from Prolog
Previous: Building Prolog terms
  Contents
  Index
To define a predicate p(a1,...,an) in C and make it callable from Prolog, follow the following steps.
- Write a C function, say p(), and store it in a file, say myCPreds.c, in the directory Emulator. As described above, the C function should take no argument. The macro ARG(i,arity) is used to fetch an argument passed from a Prolog call. The C function should return a value to the Prolog call, 1 meaning a success, 0 meaning a failure, and -1 meaning error.
- Add another function, say myCBoot(), into the file myCPreds.c, and add the following line into the body of the function.
insert_cpred("p",n,p);
The function insert_cpred establishes a relation between the Prolog predicate p and the C function p(). The name p inside the quotations is visible from Prolog, n is the arity, and the last argument p is the name of the C function defining the predicate. The name of the Prolog predicate and that of the C function are not required to be identical.
- Add myCPreds.o into the variable OBJ in Makefile and remake the B-Prolog emulator.
Next: Example
Up: Calling C from Prolog
Previous: Building Prolog terms
  Contents
  Index
Neng-Fa Zhou
1999-11-24