Node:Importing Libraries from Other Languages, Next:Exporting GPC Libraries to Other Languages, Up:Other Languages

#### Importing Libraries from Other Languages

To use a function written in another language, you need to provide an external declaration for it - either in the program, or in the interface part of a unit, or an interface module.

Let's say you want to use the following C library from Pascal:

    File `callc.c':

    #include <unistd.h>
    #include "callc.h"

    int foo = 1;

    void bar (void)
    {
      sleep (foo);
    }

    File `callc.h':

    /* Actually, we wouldn't need this header file, and could instead
       put these prototypes into callc.c, unless we want to use callc.c
       also from other C source files. */

    extern int foo;
    extern void bar (void);

Then your program can look like this:

    program CallCDemo;

    {$L callc.c}  { Or: `callc.o' if you don't have the source }

    var
      MyFoo: Integer; external name 'foo';

    procedure Bar; external name 'bar';

    begin
      MyFoo := 42;
      Bar
    end.

Or, if you want to provide a `CallCUnit` unit:

    unit CallCUnit;

    interface

    var
      MyFoo: Integer; external name 'foo';

    procedure Bar; external name 'bar';

    implementation

    {$L callc.c}  { Or: `callc.o' if you don't have the source }

    end.

    program CallCUDemo;

    uses CallCUnit;

    begin
      MyFoo := 42;
      Bar
    end.

You can either link your program manually with `callc.o` or put a compiler directive `{$L callc.o}` into your program or unit, and then GPC takes care of correct linking. If you have the source of the C library (you always have it if it is Free Software), you can even write `{$L callc.c}` in the program (like above). Then GPC will also link with `callc.o`, but in addition GPC will run the C compiler whenever `callc.c` has changed if `--automake` is given, too.

While it is often convenient, there is no must to give the C function `bar` the name `Bar` in Pascal; you can name it as you like (e.g., the variable `MyFoo` has a C name of `foo` in the example above).

If you omit the `name`, the default is the Pascal identifier, converted to lower-case. So, in this example, the `name` could be omitted for `Bar`, but not for `MyFoo`.

It is important that data types of both languages are mapped correctly onto each other. C's `int`, for instance, translates to GPC's `Integer`, and C's `unsigned long` to `MedCard`. For a complete list of integer types with their C counterparts, see Integer Types.

In some cases it can be reasonable to translate a C pointer parameter to a Pascal `var` parameter. Since const parameters in GPC can be passed by value or by reference internally, possibly depending on the system, `const foo *` parameters to C functions cannot reliably be declared as `const` in Pascal. However, Extended Pascal's `protected var` can be used since this guarantees passing by reference.

Some libraries provide a `main` function and require your program's "main" to be named differently. To achive this with GPC, invoke it with an option `--gpc-main="GPCmain"` (where `GPCmain` is an example how you might want to name the program). You can also write it into your source as a directive `{$gpc-main="GPCmain"}`.