tclからcの関数を呼ぶ(mingw)

http://www.hyuki.com/yukiwiki/wiki.cgi?Tcl
How to embed Tcl in C applications
C program that invokes a Tcl Interpreter - Tcl example

mingw用のライブラリをダウンロード

Obsolete pages Windows 95/98/2000/NT/XP: MinGW

ソースを準備

  • test.c
#include <stdio.h>
#include <tcl.h>
int square (int i) {
    return i*i;
}

static int _wrap_square(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) {
        int  _result;
        int  _arg0;
        _arg0 = (int) atol(argv[1]);
        _result = (int )square(_arg0);
        sprintf(interp->result,"%ld", (long) _result);
        return TCL_OK;
}

main (int argc, char *argv[]) {
	Tcl_Interp *myinterp;
	int status;
	myinterp = Tcl_CreateInterp();
	Tcl_CreateCommand(myinterp, "square", _wrap_square, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
	Tcl_EvalFile(myinterp, "test.tcl");
}
all:
	gcc -IC:\scilab-cvs\tcllib\include -LC:\scilab-cvs\tcllib\lib test.c -ltcl83
  • test.tcl
set a [square 10]
puts $a

結果

100