There are examples for building a dynamically loadable library for any supported Tk platform. You can find this at ftp://ftp.scriptics.com/pub/tcl/misc/example.{zip,tar.gz,tar.Z}.