A shortcut for Critcl builds C extensions on-the-fly The name stands for Compiled Runtime In Tcl.
2008-03-02 JMN Presumably the latest version of critcl can be obtained using svn co svn://svn.equi4.com/critcl/trunk critcl.vfs as mentioned below.
I say presumably because it should be noted that there are other components within the checked out structure that are out of date. (e.g md5, snit)
The critlib set of packages linked from the critcl homepage shows critcl version 0.33, whereas as at 2008-03-02 the svn source contains version 2.0
Tcl 2002 conference paper by Steve Landers and Jean-Claude Wippler is at [L1 ] as PDF (and online as HTML at [L2 ]), with slides from JCW's presentation at [L3 ].
Zarutian 14. august 2007: does critcl require an external compiler?
AK: AFAIK yes.
Zarutian 15. august 2007: got any pointers to machine code generators or compilers that dont require extensive enviroment?
AK: See http://wiki.tcl.tk/_search?S=tcc&_charset_=UTF-8 , and also Scripted Code Generation.
Critcl Manual is a first cut at trying to pull a manual together.
Below are some simple how-to examples. Critcl Examples has links to other pages.
Using inlined C code
lappend auto_path . package require critcl critcl::cproc triple {int i} int { return i * 3; /* this is C code */ } puts "three times 123 is [triple 123]"
tclkit three.tcl
Building an extension for general use
package provide four 1.0 package require critcl critcl::cproc quadruple {int i} int { return i * 4; /* this is C code */ }
cd /path/where/four.tcl/lives/ tclkit /path/to/critcl -pkg four.tcl and the result is: Source: four.tcl Library: four.so Package: /path/where/four.tcl/lives/lib/four
lappend auto_path lib package require four puts "four times 123 is [quadruple 123]"
Cross compiling using Xmingwin
Critcl supports cross compiling libraries and packages for Windows on Linux/Unix using the Xmingwin cross-compiler (based on mingw - http://www.mingw.org ).
$ cat /usr/local/bin/cross PATH=/usr/local/Xmingwin/i386-mingw32msvc/bin:$PATH export PATH exec $@
$ cross critcl -pkg four.tcl Cross compiling for Windows using Xmingwin Source: four.tcl Library: four.dll Package: /path/to/four/lib/four
tcl_platform(byteOrder) = littleEndian tcl_platform(machine) = intel tcl_platform(os) = Windows NT tcl_platform(osVersion) = 5.0 tcl_platform(platform) = windows tcl_platform(wordSize) = 4
For an example, download http://mini.net/sdarchive/critex.tar.gz , unload and change to the ex2/ directory. This contains a blowfish extension for Tcl. To build on Linux/Unix, run
$ critcl -pkg blowfish Source: blowfish.tcl Library: blowfish.so Package: /path/to/ex2/lib/blowfish
Then, to cross compile (via the above cross script) run
$ cross critcl -pkg blowfish Cross compiling for Windows using Xmingwin Source: blowfish.tcl Library: blowfish.dll Package: /path/to/ex2/lib/blowfish
If you look under ex2/lib/blowfish you'll see
lib/blowfish lib/blowfish/critcl.tcl lib/blowfish/pkgIndex.tcl lib/blowfish/Windows-x86 lib/blowfish/Windows-x86/critcl.tcl lib/blowfish/Windows-x86/blowfish.dll lib/blowfish/Linux-x86 lib/blowfish/Linux-x86/critcl.tcl lib/blowfish/Linux-x86/blowfish.so
The pkgIndex.tcl will autoload the correct binary for a particular platform.
Aug 2007 - Critcl sources are now in SVN:
svn co svn://svn.equi4.com/critcl/trunk
To create a starkit from it, also do:
ln -s critcl critcl.vfs sdx wrap critcl.kit
-jcw
Discussion below moved from Scripted Compiler to the more natural place.
AM Because of Critcl I am working on a package that abstracts the concept of a compiler and a linker away from the platform-dependencies. This way Critcl will be able to support "any" compiler/linker without the user (or the Critcl programmer) having to jump through hoops.
Vince That sounds great!
Victor Without linker ?? How then I use gcc produced *.o or archives ? That is Critcl libs should take this place. Are they encrypted ? And it is still a long way to have it accepted by many people.
NEM There is Babel, by Paul Duffin. However, like Feather, it may take a while to get hold of any code from Paul.
"How to use C and Tcl Together" [L7 ] presents Critcl [Critcl] as a profoundly important innovation.
escargo 11/11/2002: I got a 404 on this link. IDG Me too.
The link is up and functional now - AK.
escargo 12/5/2002: I see a table of contents, but no link to an article. Is it expected that the link above lead to the article itself?
Critcl makes a brief appearance in this [L8 ] column on XMingwin.
UKo How can I inject code into the package initialisation section? This is necessary to build new canvas commands or new sound subcommands for Snack.
AM There is a command cinit that allows you to do this.
UKo It isn't in the critcl wikit, is it? Where can I find an up-to-date reference? Or do I have to RTSL (Read the source Luke!)
AM I can send you the (informal) documentation I wrote ... just drop me a mail
29apr03 jcw - Here's a trick to make a file work both as Tcl script and as C source:
#undef _ /* this is mixed-mode C and Tcl source code package provide mypkg 1 package require critcl critcl::ccode { /* C code follows: */ #ifndef _TCL #include <stdio.h> /* etc ... */ #endif /* C code here ... */ #ifndef _TCL int main(int argc, char** argv) { /* standalone code here ... */ } #endif #define _ };#/* Tcl code follows: critcl::cproc proc1 {int v} int { return cdef1(v); } critcl::cproc proc2 {char* s} int { return cdef2(s); } # vim: set ft=c: */
That last line also makes the "vi" editor colorize the file as C code, which is presumably the bulk.
The idea is that you can embed large chunks of C in Tcl, with some trivial cproc definitions at the end calling that code, while keeping the file in a form which can also be used in non-Tcl environments, e.g. building code as Tcl extension *and* as a plain C application.
The other way to do this is to use "critcl::csources somefile.c" and then to store all C code there, but this stops Critcl from auto-detecting source changes made to such an external file, so its automatic recompile won't kick in. With the above approach, you can edit at will and Critcl will compile (only) when needed, while you get Tcl's context, say for running test suites. The end result can then be used in non-Tcl contexts.
Jean-Claude was aware of Perl's Inline [...] but not (?) of Python's Weave [...] in his design of Critcl.
Ffidl provides an alternative approach for "calling functions in arbitrary dynamic libraries". Also see "How to use C functions from Tcl".
wcf3 Can someone provide an example for using critcl on a MacOSX system as described in Building an extension for general use above? I have the AquaBI runtime installed and it appears to only find critcl 0.0 instead of version 0.33 that is part of the critcl.kit. The error message I get is that command critcl::crosscheck is not found (it's part of version 0.33, not 0.0 :-)
More info: The AquaBI install included a set of example critcl files called critlib that required critcl 0.30 and forced the loading of the older version 0.0. By removing the critlib stuff from /Library/Tcl I am now able to get the proper critcl 0.33 loaded. The problem now is the include files used by critcl' are for X windows and not Aqua...duh, I should have seen that one coming. Any ideas?
CMcC Critcl needs to allow you to specify arbitrary compiler arguments, which would enable you to use the -I command to select your preferred inclusion places. At the moment, this is not possible.
About the only thing I can suggest is using the critcl::cheaders command to specify, individually, the files you want to include (and the files *they* want to include, in some cases) in the correct order.
It's quite annoying.
stevel I regularly do things like
critcl::cheaders -I[pwd] critcl::cheaders -L[pwd] -lmylib
Lars H, 8 Feb 2006: Some info on the Critcl/Aqua issue can be found in [L9 ]. The important part for simple uses appears to be the command
critcl::cheaders -DMAC_OSX_TK -I/Library/Frameworks/Tk.framework/Headers
(The -I option can probably be changed so that one doesn't have to specify an explicit path to the Tk framework, but I haven't tried that yet.)
critcl::cdefines - stevel Dec 17 2004
critcl::cdefines allows C #defines and enums to be mapped from C into a Tcl namespace.
For example, Cryptkit uses the following to map Cryptlib symbols (i.e. #defines and enums) into the cryptkit namespace
# map Cryptlib #defines and enums into the current namespace critcl::cdefines CRYPT_* [namespace current] # other defines critcl::cdefines { NULL TRUE FALSE TCL_OK TCL_ERROR } [namespace current]
RLH 2009-04-11: Is Critcl going to be in teapot (for OSX)? I remember there being a problem with that platform. I think...
AK Critcl is not in the teapot, for no platform. Are you possibly confusing critcl with tcllibc ? That is a shared library created from a number of critcl based code found Tcllib and accelerating a few packages of it.
stevel further to that, Critcl works just fine on OSX - it even generates universal binaries by default
See also photo image equality in Critcl for how to speed up Tcl code 1200 times :^) - and Odyce, tcltcc which partially emulate the Critcl API.