extension example

extension example is the most simple example of a Tcl extension. More examples at Extending Tcl.

Contributed by Ralf Fassel, stubs support added by George Peter Staplin

/* hello.c */
#include <stdio.h>
#include <stdlib.h>
#include <tcl.h>
hello(/* args ignored */)
  printf("hello world\n");
  return TCL_OK;
Hello_Init(Tcl_Interp *interp)
  if (NULL == Tcl_InitStubs (interp, TCL_VERSION, 0))
    return TCL_ERROR;

  printf("this is hello loading\n");

  Tcl_CreateCommand(interp, "hello", hello, NULL, NULL);
  return Tcl_PkgProvide(interp, "Hello", "1.0");
% gcc -fPIC -shared -o /tmp/hello.so hello.c
% tclsh
% load /tmp/hello.so 

For stubs:

$ gcc -shared -o hello.so -DUSE_TCL_STUBS hello.c /path/to/libtclstub84.a -lm -Wl,-rpath=/path/to/dir/with/libtcl84.so
$ tclsh8.4
% load ./hello.so