next up previous
Up: User's Manual of the Previous: 7. Standard modules and

  
8. Writing modules in C

Sometimes it is needed to write a module in C, for instance to interface to a library which is not available for Orca, but is available for C. If this module is called modnam, the user must write a specification module modnam.spf which contains Orca specifications for all functions that the user wants to write in C. The oc program will compile this specification, because it detects that there is no corresponding module implementation in Orca. It will produce a modnam.c and a modnam.h file for it.

The user must of course also provide the C implementation of these functions. These must reside in a .c file (with a different name!) that must start with

#include <interface.h>
#include "modnam.h"
Each function f must have a corresponding implementation in C with the name
f_modnam__f
If the function does not return a value, or it returns a complex type, it must be declared void. If it returns a simple type, the following table indicates which type to use for each simple Orca type.

Orca type C type usual C implementation
shortint t_shortint short
integer t_integer int
longint t_longint long
shortreal t_shortreal float
real t_real double
longreal t_longreal double
char t_char unsigned char
enum t_enum unsigned char
boolean t_boolean unsigned char

OUT or SHARED parameters are passed by reference (an address is passed), IN parameters are passed by value if they have a simple type, otherwise they are passed by reference as well. (This means that the user has to be careful not to change IN parameters.) In addition, if the function returns a complex type, a parameter should be added as if it is an OUT parameter.

As a small example, if the user wants to use the exp function of C, he could have the following specification module math.spf:

MODULE SPECIFICATION math;
    FUNCTION exp(x: REAL): REAL;
END;
and the following exp.c file:
#include <interface.h>
#include "math.h"

double exp(double);

t_real f_math__exp(t_real x)
{
    return (t_real) exp((double) x);
}

The user should also explicitly add exp.c to the list of files offered to oc.


next up previous
Up: User's Manual of the Previous: 7. Standard modules and
Ceriel JH Jacobs
1999-04-22