Critcl builds C extensions on-the-fly

Source code is now at http://www.equi4.com/critlib/ - JCW

  • Version 0.18 now runs on Linux and Windows (MinGW)
  • As of 0.22, you can store the binaries generated in a specific directory with a more meaningful name
  • source is now up to 0.34 (2006-Jun-09) Lars H: Eh? I downloaded version 0.35 months ago.

(See also Richard Suchenwirth's C code generators page.)


The C Runtime In Tcl is a self-contained package to build C code into an extension on the fly. It is somewhat inspired by Brian Ingerson's Inline for Perl, but Critcl is considerably more lightweight. [While Python also has an Inline [L1 ], it apparently has no direct genetic connection to Critcl.]

The idea is to wrap C code into something that will compile into a Tcl extension, and then also fire up the compiler. Compiled chunks will be cached in your ~/.critcl/ directory, so subsequent use will be instant.


Critcl Wiki Resources:


Using Critcl:

The main definition is "critcl::cproc", which lets you define a (surprise) C proc, with C code as body. Args and return values must be typed. There are no default args or ways to pass more sophisticated data items than int/long/float/double/char* for now.

There is also a "critcl::ccode" call, to inject C code as is, and a "ccommand" call, which ties code to the Tcl_CreateObjCommand without further wrapping.

The use of Tcl stubs, and the fact that this extension has all include files it needs to make compilation self-contained, means that this is a pure Tcl package, which should work with any (8.1 and up) installation of Tcl. Most importantly, Critcl does not care a bit where Tcl was installed, nor even whether it was built as a static or as a dynamic executable.

This is a working demo, but it is still young. (It's now rather more mature - KJN) It will for now blindly use "gcc" to do the compile and has somewhat rudimentary error handling. Critcl has been verified to work on Linux and Win NT4 (MinGW) so far.

The required md5 command and other dependencies are provided as part of the Critcl starpack.

There's a new CritLib package with several extensions (blowfish, hexdump, ihash, lazy, lzrw1, md5c, mvector, noop, rchan, zlib) - see http://www.equi4.com/critlib/

More news on newer rev's of Critcl, or rather the "CritLib" where it is included:

  • There's a critcl::config to specify things like: outdir - putting all compiled extensions in a fixed dir, using a normal (non-MD5-ized name), and keepsrc - do not delete the generated intermediate C source code. See the Critcl Manual for details.
  • There's a critbind script which takes one or more Critcl packages, and turns them into a single deliverable: either a loadable extension, or a static lib (for linking later), or an application. The latter is usually called a "custom tclsh", it can be built either as small main linked to Tcl dynamically, or as bigger exe with all of Tcl linked in statically

JCW


Critcl Articles:

Conference paper by Steve Landers and Jean-Claude Wippler is at [L2 ] (PDF) and [L3 ] (HTML online), with slides from JCW's presentation at [L4 ].

A dated article [L5 ] has background information on interpreted C.


AK: Larry Virden provided this pointer:

http://www.purl.org/NET/Tcl-FAQ/part4.html

 What: Embedded C (EC)
 Where: ftp://ftp.reed.edu/pub/users/greaber/ec-0.1.tar.gz 
 Description: Allows you to include C code in your Tcl scripts, compiling
        and dynamically loading it on the fly.  The code will also be
        cached so the next time you run the program, you don't wait for
        it to compile.  Known to work on DEC OSF/1 V3.2 and SunOS 5.5.
        Not yet ported to Macintosh or Windows.
 Updated: 09/1996
 Contact: mailto:[email protected]  (Grant Reaber)

George Petasis: There is a small tcl extension (tBuild - http://sourceforge.net/projects/tbuild/ ) which will compile and link a Tcl C extension by looking in the tclConfig.sh under unix and detecting visual studio from the registry under windows. It would be nice addition to Critcl to build code with, and not be limited to only gcc under unix.