TCL Wrapping Using C

For fast developing of some algorithms, TCL is one of best choices specially for developes working in semiconductors area. In some cases, developers need to convert their TCL scripts into standalone executable. This task can be easily achieved if the developer knows which C functions can be used. This tutorial is explaining this step in details, scrol down at end of page to read this tutorial in PDF

To skip reading this background, free utility tclpot is provided in here which can be used to convert your TCL script into executable.

Nevertheless, here is simple primitive template that can be used to embed your TCL script in C

/* ******************* Begin of Template ************************** */
#include <stdio.h>
#include <stdlib.h> 
#include <tcl.h>
 
#define CMD_MAX_SIZE 256
 
static const char *user_script =  
 "puts \"Hello World …….\"\n" 
 ;

int exe_tcl_cmd(Tcl_Interp *interp, const char *arg_str, …){   
 char *cmd_str = Tcl_Alloc(CMD_MAX_SIZE);   
   va_list ap;   
   int result=0;   
   va_start(ap,arg_str);   
   vsprintf(cmd_str, arg_str, ap);   
   result = Tcl_Eval(interp,cmd_str);   
   Tcl_Free(cmd_str);
   va_end(ap);
 return result; 
 }
 
int main (int argc ,char *argv[]) {   
   Tcl_FindExecutable(argv[0]);     
   Tcl_Interp *myinterp;     
   printf ("C: Starting … \n");    
   myinterp = Tcl_CreateInterp();
 
   if (Tcl_Init(myinterp) != TCL_OK) {         
     printf("Error: %s\n",Tcl_GetStringResult(myinterp));         
     return TCL_ERROR;     
   }

   if (exe_tcl_cmd(myinterp, user_script) != TCL_OK) {         
     printf("Error: %s\n",Tcl_GetStringResult(myinterp));         
     return TCL_ERROR;     
   }
   Tcl_DeleteInterp(myinterp);     
   Tcl_Finalize();     
   printf ("C: Finished\n");
   return TCL_OK; 
 }
/* ******************* End of Template ************************** */