首次按一定要安装好环境,下载tcl8.5就可以,直接。./configure&&make&&make install 即可。
一、test.c
#include <tcl.h>
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <math.h>
int
PowObjCmd(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
Tcl_Obj *resultptr;
double x,y,result;
int error;
if (objc != 3) {
Tcl_WrongNumArgs(interp,2,objv,
"Usage : pow x y");
return TCL_ERROR;
}
error = Tcl_GetDoubleFromObj(interp, objv[1], &x);
if (error != TCL_OK) return error;
error = Tcl_GetDoubleFromObj(interp, objv[2], &y);
if (error != TCL_OK) return error;
result = pow(x,y);
resultptr = Tcl_GetObjResult(interp);
Tcl_SetDoubleObj(resultptr,result);
return TCL_OK;
}
int
Example_Init(Tcl_Interp *interp) {
Tcl_CreateObjCommand(interp, "pow", PowObjCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
return TCL_OK;
}
二、makefile
#include <tcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int
PowObjCmd(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
Tcl_Obj *resultptr;
double x,y,result;
int error;
if (objc != 3) {
Tcl_WrongNumArgs(interp,2,objv,
"Usage : pow x y");
return TCL_ERROR;
}
error = Tcl_GetDoubleFromObj(interp, objv[1], &x);
if (error != TCL_OK) return error;
error = Tcl_GetDoubleFromObj(interp, objv[2], &y);
if (error != TCL_OK) return error;
result = pow(x,y);
resultptr = Tcl_GetObjResult(interp);
Tcl_SetDoubleObj(resultptr,result);
return TCL_OK;
}
int
Example_Init(Tcl_Interp *interp) {
Tcl_CreateObjCommand(interp, "pow", PowObjCmd,
(ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
return TCL_OK;
}
三、test.tcl
#!/usr/bin/tclsh
load ./libtest.so example
pow 2 4