SWIG:Examples:tcl:simple

`SWIG/Examples/tcl/simple/`

## Simple Tcl Example

This example illustrates how you can hook Tcl to a very simple C program containing a function and a global variable.

## The C Code

Suppose you have the following C code:

>     /* File : example.c */

>     /* A global variable */
>     double Foo = 3.0;

>     /* Compute the greatest common divisor of positive integers */
>     int gcd(int x, int y) {
>       int g;
>       g = y;
>       while (x > 0) {
>         g = x;
>         x = y % x;
>         y = g;
>       }
>       return g;
>     }

## The SWIG interface

Here is a simple SWIG interface file:

>     /* File: example.i */
>     %module example

>     extern int gcd(int x, int y);
>     extern double Foo;

## Compilation

1. `swig -tcl example.i`

2. Compile `example_wrap.c` and `example.c` to create the extension `example.so`.

## Using the extension

Click here to see a script that calls our C functions from Tcl.

## Key points

- Use the `load` statement to load your extension module into Tcl. For example:

  >     load ./example.so

- C functions work just like Tcl functions. For example:

  >     set g [gcd 42 105]

- C global variables are accessed as Tcl variables. For example:

  >     set a $Foo
  >     set Foo $newvalue