Advertisement
devincpp

use_tcl_in_cpp

Sep 19th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. // tcltest.cpp: 定义应用程序的入口点。
  2. //
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <tcl.h>
  8. Tcl_Interp *interp;
  9. int PowObjCmd(ClientData clientData, Tcl_Interp *interp, int objc,
  10.               Tcl_Obj *CONST objv[]) {
  11.   Tcl_Obj *resultptr;
  12.   double x, y, result;
  13.   int error;
  14.  
  15.   if (objc != 3) {
  16.     Tcl_WrongNumArgs(interp, 2, objv, "Usage : pow x y");
  17.     return TCL_ERROR;
  18.   }
  19.   error = Tcl_GetDoubleFromObj(interp, objv[1], &x);
  20.   if (error != TCL_OK)
  21.     return error;
  22.   error = Tcl_GetDoubleFromObj(interp, objv[2], &y);
  23.   if (error != TCL_OK)
  24.     return error;
  25.  
  26.   result = pow(x, y);
  27.   resultptr = Tcl_GetObjResult(interp);
  28.   Tcl_SetDoubleObj(resultptr, result);
  29.   return TCL_OK;
  30. }
  31. int Example_Init() {
  32.   interp = Tcl_CreateInterp();
  33.   if (interp == NULL) {
  34.     return 1;
  35.   }
  36.   Tcl_CreateObjCommand(interp, "pow111", PowObjCmd, (ClientData)NULL,
  37.                        (Tcl_CmdDeleteProc *)NULL);
  38.   return TCL_OK;
  39. }
  40. int main() {
  41.   int ret;
  42.   Example_Init();
  43.   Tcl_Eval(interp, "puts {hello world!!!}");
  44.   Tcl_Eval(interp, "set a 123");
  45.   Tcl_Eval(interp, "puts [expr $a +23]");
  46.   Tcl_Eval(interp, "puts [pow111 2 3]");
  47.   ret = Tcl_EvalFile(interp, "test.tcl");
  48.   if (ret == TCL_ERROR) {
  49.     printf("Tcl  script  error :%d", interp->errorLineDontUse);
  50.     // DataLog::insertERROR(logdata);
  51.   }
  52.   printf("ok %d\n", ret);
  53.   return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement